Rewrite code in V8

31 views
Skip to first unread message

teem...@gmail.com

unread,
May 24, 2019, 6:01:34 PM5/24/19
to v8-dev
Hi all,

I'm trying to create some automatic rewriting of all the JS code that is being parsed by Chrome/V8. For that I went to ParseInfo::CreateScript and just replaced the source string with my rewritten one:

    std::string s = source->ToCString().get();
    std::string transformed = rewrite(isolate, s); // rewrite is implemented somewhere else.
    v8::internal::Factory* factory = isolate->factory();
    v8::internal::Vector<const char> vec(transformed.data(), static_cast<size_t>(transformed.size()));
    source = factory->NewStringFromUtf8(vec).ToHandleChecked();

However that seems to mess with the rest of the parsing process, because the first few hundred characters of my source go randomly missing when the parser starts parsing (I couldn't figure that bug out yet).

Are there any better functions where I can inject this rewriting logic, e.g. some central parsing function where it's safer to replace the string?

(Note that I only want to create some proof-of-concept, not something that would ever go into production).

Cheers,
- Raphael

Ben Noordhuis

unread,
May 25, 2019, 4:39:14 AM5/25/19
to v8-...@googlegroups.com
I'd tackle that at the API boundary.
ScriptCompiler::CompileUnboundInternal() in src/api.cc should cover
all C++ entry points.

Compiler::GetFunctionFromEval() in src/compiler.cc is probably the
method you want to patch if you also want to intercept eval() and
Function().
Reply all
Reply to author
Forward
0 new messages