How do I include a javascript function inside of embedded v8?

38 views
Skip to first unread message

Renier

unread,
Sep 6, 2016, 8:37:33 AM9/6/16
to v8-users
I have an application that embeds v8 and I want to include a series of helper javascript functions at the time of compiling a code snippet so that the user is able to use my helper functions.
These functions don't interact with C++ at all and are merely there as helper functions.

i.e.
function doStuff(x, y){
   
return x + y + "weeee";
}
function doSomethingElse(){
}


Is there a sample of the above?

I have tried to use an v8::ObjectTemplate and merely doing the following without any success as I get `Reference error: doSomethingElse is not defined`

v8::Handle<v8::ObjectTemplate> result = v8::ObjectTemplate::New();
result->Set(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), *functionName), v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), *functionSource));

functionName is set to "doStuff" and the definition of the function lives inside of the functionSource.

I have then created a context with this template and I get the above mentioned error message when I attempt to access these functions.

Regards
Renier

Ben Noordhuis

unread,
Sep 6, 2016, 8:53:51 AM9/6/16
to v8-users
Compile (and run) the source with v8::Script or v8::ScriptCompiler.

You could also use v8::RegisterExtension() but that's a bit more work.
Message has been deleted

Renier

unread,
Sep 6, 2016, 9:00:31 AM9/6/16
to v8-users
Thanks for the quick reply Ben, but i'm not sure i'm following

Renier

unread,
Sep 6, 2016, 9:45:20 AM9/6/16
to v8-users
Are you saying I should compile each script in the context of where I will run the user's js?

Abhishek Singh

unread,
Sep 6, 2016, 10:29:28 AM9/6/16
to v8-u...@googlegroups.com
Before compilation of user supplied JS code(assuming you’re storing it as std::string), append the helper functions to it(which you might be reading from a file sitting in some predefined location) and finally compile the resultant std::string(in below snippet assuming “src” stores the resultant final string). 

Local<String> source = String::NewFromUtf8(isolate, src.c_str(), NewStringType::kNormal).ToLocalChecked();
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
Local<Value> result = script->Run(context).ToLocalChecked();

You could refer to process.cc within V8 src, which does something similar amount various other things.


On 06-Sep-2016, at 7:15 PM, Renier <germishu...@gmail.com> wrote:

Are you saying I should compile each script in the context of where I will run the user's js?

--
--
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.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages