Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove test fixture compileAndRunScript and compileAndInstantiateModule #1249

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 2 additions & 71 deletions src/workerd/tests/test-fixture-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ KJ_TEST("runInIoContext re-throwing js exception") {
try {
fixture.runInIoContext([&](const TestFixture::Environment& env) -> kj::Promise<void> {
runCount++;
env.compileAndRunScript("throw new Error('let_me_through');");
return kj::READY_NOW;
env.js.throwException(env.js.error("let_me_through"));
}, kj::arr("test_error"_kj));
} catch (kj::Exception& e) {
KJ_EXPECT(e.getDescription() == "jsg.Error: let_me_through"_kj);
Expand All @@ -132,80 +131,12 @@ KJ_TEST("runInIoContext consuming ignored js exception") {

fixture.runInIoContext([&](const TestFixture::Environment& env) -> kj::Promise<void> {
runCount++;
env.compileAndRunScript("throw new Error('test_error');");
return kj::READY_NOW;
env.js.throwException(env.js.error("test_error"));
}, kj::arr("test_error"_kj));

KJ_EXPECT(runCount == 1);
}

KJ_TEST("compileAndRunScript") {
TestFixture fixture;
uint runCount = 0;

fixture.runInIoContext([&](const TestFixture::Environment& env) {
runCount++;
auto result = env.compileAndRunScript("42;");
v8::String::Utf8Value value(env.isolate, result);
KJ_EXPECT(*value == "42"_kj);
});
KJ_EXPECT(runCount == 1);
}

KJ_TEST("compileAndRunScript context access") {
TestFixture fixture;
uint runCount = 0;

fixture.runInIoContext([&](const TestFixture::Environment& env) {
runCount++;
auto result = env.compileAndRunScript("btoa([1,2,3,4,5]);");
v8::String::Utf8Value value(env.isolate, result);
KJ_EXPECT(*value == "MSwyLDMsNCw1"_kj);
});
KJ_EXPECT(runCount == 1);
}

KJ_TEST("compileAndRunScript exception handling") {
TestFixture fixture;
uint runCount = 0;
uint exceptionCount = 0;

try {
fixture.runInIoContext([&](const TestFixture::Environment& env) -> kj::Promise<void> {
runCount++;
env.compileAndRunScript("throw new Error('test_error');");
KJ_FAIL_REQUIRE("shouldn't happen");
});
} catch (kj::Exception& e) {
exceptionCount++;
KJ_EXPECT(e.getDescription() == "jsg.Error: test_error"_kj);
}

KJ_EXPECT(runCount == 1);
KJ_EXPECT(exceptionCount == 1);
}

KJ_TEST("compileAndInstantiateModule") {
TestFixture fixture;
uint runCount = 0;

fixture.runInIoContext([&](const TestFixture::Environment& env) {
runCount++;
auto context = env.isolate->GetCurrentContext();

auto ns = env.compileAndInstantiateModule("testFixtureTest",
"export function init() { return 42; }"_kj);
auto fn = env.js.v8Get(ns, "init"_kj);
KJ_EXPECT(fn->IsFunction());
auto callResult = v8::Function::Cast(*fn)->
Call(context, context->Global(), 0, nullptr).ToLocalChecked();
v8::String::Utf8Value value(env.isolate, callResult);
KJ_EXPECT(*value == "42"_kj);
});

KJ_EXPECT(runCount == 1);
}

KJ_TEST("runRequest") {
TestFixture fixture({
.mainModuleSource = R"SCRIPT(
Expand Down
37 changes: 0 additions & 37 deletions src/workerd/tests/test-fixture.c++
Original file line number Diff line number Diff line change
Expand Up @@ -340,41 +340,4 @@ TestFixture::Response TestFixture::runRequest(
return { .statusCode = response.statusCode, .body = response.body->str() };
}

v8::Local<v8::Value> TestFixture::V8Environment::compileAndRunScript(
kj::StringPtr code) const {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::String> source = jsg::v8Str(isolate, code);
v8::Local<v8::Script> script;
if (!v8::Script::Compile(context, source).ToLocal(&script)) {
KJ_FAIL_REQUIRE("error parsing code", code);
}

v8::TryCatch catcher(isolate);
v8::Local<v8::Value> result;
if (script->Run(context).ToLocal(&result)) {
return result;
} else {
KJ_REQUIRE(catcher.HasCaught());
catcher.ReThrow();
throw jsg::JsExceptionThrown();
}
}

v8::Local<v8::Object> TestFixture::V8Environment::compileAndInstantiateModule(
kj::StringPtr name, kj::ArrayPtr<const char> src) const {
v8::Local<v8::Module> module;

v8::ScriptCompiler::Source source(jsg::v8Str(isolate, src),
v8::ScriptOrigin(isolate, jsg::v8StrIntern(isolate, name),
false, false, false, -1, {}, false, false, true /* is_module */));

if (!v8::ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
KJ_FAIL_REQUIRE("error parsing code");
}

auto& js = jsg::Lock::from(isolate);
jsg::instantiateModule(js, module);
return module->GetModuleNamespace()->ToObject(isolate->GetCurrentContext()).ToLocalChecked();
}

} // namespace workerd
7 changes: 0 additions & 7 deletions src/workerd/tests/test-fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ struct TestFixture {

struct V8Environment {
v8::Isolate* isolate;

// Compile and run the script. Returns the result of last statement.
v8::Local<v8::Value> compileAndRunScript(kj::StringPtr script) const;

// Compile and instantiate esm module. Returns module namespace object.
v8::Local<v8::Object> compileAndInstantiateModule(
kj::StringPtr name, kj::ArrayPtr<const char> src) const;
};

struct Environment : public V8Environment {
Expand Down
Loading