On Sat, Aug 28, 2021 at 9:45 PM Ronald Fenner <rfen...@gamecircus.com> wrote:
>
> I'm trying to test a JS Module that creates a new object that's been 
> registered from the C++ side but it fails on evaluation with:
> Reference Error: TestObj is not defined.
>
> I can run a script that does essentially the same thing as the module and 
> it's able to run successfully and trigger the C++ function for the 
> construction of the object.
>
> Here's the test code
>
> v8::HandleScope handleScope(m_Isolate);
> JSContext *jsContext = m_Context.lock().get();
> WeakJSModulesPtr weakJSModules = jsContext->GetModules();
> JSModules *jsModules = weakJSModules.lock().get();
>
> JSModulesTestInternal::TestObject::BuildObjectTemplate(m_Isolate);
>
> const char source[] = R"script(
> let obj = new TestObject();
> )script";
> v8::Local<v8::String> v8Source = JSUtilities::StringToV8(m_Isolate, source);
> EXPECT_FALSE(v8Source.IsEmpty());
>
> v8::TryCatch tryCatch(m_Isolate);
>
> v8::Local<v8::Script> script = v8::Script::Compile(jsContext->GetContext(), 
> v8Source).ToLocalChecked();
> script->Run(jsContext->GetContext());
> if (tryCatch.HasCaught())
> {
> std::string error = JSUtilities::GetStackTrace(m_Isolate, tryCatch);
> std::cout << "Script Error: " << error << std::endl;
> ASSERT_TRUE(false);
> }
>
> EXPECT_NE(nullptr, JSModulesTestInternal::constructerCreatedObjectTest);
> JSModulesTestInternal::constructerCreatedObjectTest) = nullptr;
>
> //test that module can't be instantiate cause imported module isn't loaded
> v8::Local<v8::Module> root;
> v8::MaybeLocal<v8::Module> maybeRoot = jsModules->LoadModule("root.mjs");
> EXPECT_TRUE(maybeRoot.ToLocal(&root));
> ASSERT_FALSE(root.IsEmpty());
>
> EXPECT_TRUE(jsModules->InstantiateModule(root));
>
> EXPECT_TRUE(jsModules->RunModule(root)); // Fails here.
>
> ASSERT_NE(nullptr, JSModulesTestInternal::constructerCreatedObjectTest);
>
> EXPECT_EQ(5, JSModulesTestInternal::constructerCreatedObjectTest->GetValue());
>
> Here's the modules code
> import rootImport from 'sub-dir/rootImport'
>
> let obj = new TestObj();
> obj.value = 5;

You didn't include the actual instantiation and evaluation code so
it's hard to say what's going wrong. Try reducing it to a minimal
demonstration.

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/CAHQurc93C2FHo18rHcNMsP%3Da8BH5jk-gDVgj_tGgprS33XyPQg%40mail.gmail.com.

Reply via email to