JS Module can't see registered function template.

31 views
Skip to first unread message

Ronald Fenner

unread,
Aug 28, 2021, 3:45:33 PM8/28/21
to v8-users
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;


Ben Noordhuis

unread,
Aug 29, 2021, 5:20:59 AM8/29/21
to v8-users
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.

Camillo Bruni

unread,
Sep 15, 2021, 3:17:13 AM9/15/21
to v8-u...@googlegroups.com
Hi Ronald,

I'm having a hard-time understanding exactly where you create the TestObj class/constructor since
much of your implementation seems to be hidden in JSModules and JSModulesTestInternal
Could you maybe create an example of your issue with pure V8 API?

cheers,
Camillo

--
--
v8-users mailing list
v8-u...@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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/fbbc9546-d83a-4195-983a-db773d6a1745n%40googlegroups.com.
Camillo Bruni | Software Engineer, V8 | Google Germany GmbH | Erika-Mann Str. 33, 80636 München 

Registergericht und -nummer: Hamburg, HRB 86891 | Sitz der Gesellschaft: Hamburg | Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

Diese E-Mail ist vertraulich. Falls Ssie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde.  This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.
Reply all
Reply to author
Forward
0 new messages