4

I'm trying to embed an custom function to my project, that uses the V8 engine, and apparently I can't make it working. I've used code, that I've found, but it seems to be outdated, or I just do it wrong. My point is to include a custom javascript file. My current code (for testing) is this :

    HandleScope handle_scope(isolate);

    v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
    global->Set(v8::String::NewFromUtf8(isolate, "test", v8::NewStringType::kNormal).ToLocalChecked(),
        v8::FunctionTemplate::New(isolate, test));

    Handle<Context> context = Context::New(isolate);
    Persistent<Context> persistent_context(isolate, context);

    Context::Scope context_scope(context);
    const char* data = "test";
    Handle<String> source = String::NewFromUtf8(isolate, data);

    Handle<Script> script = Script::Compile(source);
    if (!script.IsEmpty())
        Handle<Value> result = script->Run();

Test Code (obviously just for testing):

void test(const v8::FunctionCallbackInfo<v8::Value>& args) {
    MessageBoxA(NULL,"test", "", 0);
}

But the engine returns this error :

Uncaught ReferenceError: test is not defined

So my question is if I even do it correct, I would be able to make the including myself I hope, but I just can't get the function to get executed.

1 Answer 1

2

Here's some code from a project that works:

Isolate::Scope iscope( isolate_ );
HandleScope hs( isolate_ );


Local<Object> test = Object::New(isolate_);
test->Set(String::NewFromUtf8(isolate_, "javaScriptFunctionName"), Function::New(isolate_, CPP_FN_CALLBACK));
global_template->Set(String::NewFromUtf8(isolate_, "test"), test);

That will result in an object for window.test with a function called window.test.javaScriptFunctionName()

Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate on CPP_FN_CALLBACK please? Is it a standard function like you see in the addon's entry point?
@JacobSchneider Yes, exactly. Just a standard cpp function that takes int v8 args as a parameter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.