Exporting a function in a synthetic module

118 views
Skip to first unread message

stijnf...@gmail.com

unread,
Apr 29, 2020, 10:02:05 AM4/29/20
to v8-dev
Hi everyone,

I'm trying to define a synthetic module exporting a function, but I'm running into some problems.

1. Am I correct in assuming I have to add any exported values in the SyntheticModuleEvaluationSteps function? I can only call SetSyntheticModuleExport() within that call? This isn't clearly documented everywhere.

2. Exporting simple values like integers works fine, but not functions. Here's my code and the corresponding error:

v8::MaybeLocal<v8::Value> ScriptEngine::fillModule(v8::Local<v8::Context> context, v8::Local<v8::Module> module)
{
    auto isolate = context->GetIsolate();
    
    auto name = v8::String::NewFromUtf8(isolate, "now").ToLocalChecked();
    module->SetSyntheticModuleExport(name, v8::FunctionTemplate::New(isolate, &getNow)->GetFunction(context).ToLocalChecked());
    
    return v8::True(context->GetIsolate());
}

#

# Fatal error in , line 0

# Check failed: export_object->IsCell().

#

#

#

#FailureMessage Object: 0x700000178d10


What am I missing here?

Georg Neis

unread,
Apr 29, 2020, 10:19:05 AM4/29/20
to v8-...@googlegroups.com
On Wed, Apr 29, 2020 at 4:02 PM <stijnf...@gmail.com> wrote:
> 1. Am I correct in assuming I have to add any exported values in the SyntheticModuleEvaluationSteps function? I can only call SetSyntheticModuleExport() within that call? This isn't clearly documented everywhere.

It could theoretically also get called later.

> 2. Exporting simple values like integers works fine, but not functions. Here's my code and the corresponding error:
>
> v8::MaybeLocal<v8::Value> ScriptEngine::fillModule(v8::Local<v8::Context> context, v8::Local<v8::Module> module)
> {
> auto isolate = context->GetIsolate();
>
> auto name = v8::String::NewFromUtf8(isolate, "now").ToLocalChecked();
> module->SetSyntheticModuleExport(name, v8::FunctionTemplate::New(isolate, &getNow)->GetFunction(context).ToLocalChecked());
>
> return v8::True(context->GetIsolate());
> }

This code looks unsafe in that the module Local can get dereferenced
before the FunctionTemplate allocation, in which case the resulting
raw address might be invalid after the allocation. Move the allocation
into a separate statement before the call to SetSyntheticModuleExport.
If that doesn't solve your problem please file a bug on the V8 issue
tracker and include code for reproducing it.

--
Georg Neis
Software Engineer

Google Germany GmbH
Erika-Mann-Straße 33
80636 München

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

Diese E-Mail ist vertraulich. Falls sie 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.

stijnf...@gmail.com

unread,
Apr 29, 2020, 10:40:03 AM4/29/20
to v8-dev
Thanks for you reply, Georg.

How exactly can the module local get dereferenced before the call? I reckon you mean the FunctionTemplate local?

In any case, moving the FunctionTemplate creation to a separate line before that doesn't solve the problem.
I'll file a report with the issue tracker, then :)

stijnf...@gmail.com

unread,
Apr 29, 2020, 10:57:40 AM4/29/20
to v8-dev
Ah, I think I found the issue (right before I was to file the issue).

There are two varieties of SetSyntheticModuleExport(), though only one of them is mentioned in the docs (even the latest).
Looking at the v8 headers, the one taking two arguments is soon to be deprecated for one taking an Isolate.

Using the second one gives better error reporting (in my case I forgot the mention the function name in CreateSyntheticModule()).
Reply all
Reply to author
Forward
0 new messages