What's the difference between these two JS codes in V8?

56 views
Skip to first unread message

haochen shi

unread,
Nov 20, 2023, 9:55:14 PM11/20/23
to v8-dev
I try to create a codeCache via V8.

```
ScriptCompiler::CachedData* CreateCachedData(Local<String> sourceContent, Local<String> sourceName) {
    ScriptOrigin* scriptOriginPtr = nullptr;
    scriptOriginPtr = new ScriptOrigin(
        String::NewFromTwoByte(isolate_, sourceName, NewStringType::kNormal).ToLocalChecked(),
        Integer::New(isolate_, 0), Integer::New(isolate_, 0));
    ScriptCompiler::Source* source = nullptr;
    source = new ScriptCompiler::Source(sourceContent, *scriptOriginPtr);

    Local<Script> script;
    ScriptCompiler::Compile(context_, source, ScriptCompiler::CompileOptions::kEagerCompile).ToLocal(&script);

    ScriptCompiler::CachedData* data = ScriptCompiler::CreateCodeCache(script->GetUnboundScript());

    return data;
}
```

When I use these code, the value of data is nullptr.

```
function A() {
    "use asm"
    function R() {}
    return R
}
```

When I use these code, the value of data isn't nullptr.

```
function A() {
    "use asm"
    function R() {};
    return R
}
```

What's the difference between these two JS codes? In V8? And In JS?

Clemens Backes

unread,
Nov 21, 2023, 5:41:06 AM11/21/23
to v8-...@googlegroups.com
The difference is that the first snippet is valid asm.js while the second snippet is not.

d8 prints a message if you try to instantiate the second function (via `A()`):
test.js:3: Invalid asm.js: Unexpected token

asm.js code is internally converted to Wasm and then run through the Wasm pipeline. There is a TODO in the code to enable serialization of asm.js modules, but this has not been worked on in the last years. If this is something you need, feel free to open a bug and describe your use case.

-Clemens

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/5c6eaaa6-b0cc-4196-bd90-2d2dbe632f21n%40googlegroups.com.


--

Clemens Backes

Software Engineer

clem...@google.com

Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Liana Sebastian   

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.


haochen shi

unread,
Nov 22, 2023, 3:36:32 AM11/22/23
to v8-dev
Thanks for the reply.
But I'm curious about how you got this error message 'test.js:3: Invalid asm.js: Unexpected token' via d8.
When I run these two snippets, I don't get any error message.

130|HWSEA-A:/data/local/tmp/v8/bin $ ./d8
V8 version 9.7.0 (candidate)
d8> function A() {"use asm"; function R() {} return R}
undefined
d8> function B() {"use asm"; function R() {}; return R}
undefined
d8>

Clemens Backes

unread,
Nov 22, 2023, 4:09:06 AM11/22/23
to v8-...@googlegroups.com
You need to invoke the function (to instantiate the asm.js module). Try this:
function B() {"use asm"; function R() {}; return R}; B()

Reply all
Reply to author
Forward
0 new messages