0

I am trying to make some functions defined in c++ code available to JavaScript code running on v8.

Following some examples found over the web I was lead to believe that the following should work:

#include <stdio.h>
#include <v8.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

v8::Handle< v8::Value > print( const v8::Arguments & args ) {
   for ( int i = 0; i < args.Length(); i++ )
   {
      v8::String::Utf8Value str( args[ i ] );
      std::cout << *str;
   }

   return v8::Undefined();
}

int main( int argc, char* argv[] ) {

   // Create a stack-allocated handle scope.
   v8::HandleScope handle_scope;

   // Make "print" available:
   v8::Handle< v8::ObjectTemplate > global = v8::ObjectTemplate::New();
   global->Set( v8::String::New( "print" ), v8::FunctionTemplate::New( print ) );

   // Create a new context.
   v8::Handle< v8::Context > context = v8::Context::New();
   // Enter the created context for compiling and
   // running the hello world script.
   v8::Context::Scope context_scope( context );

   // Create a string containing the JavaScript source code.
   v8::Handle< v8::String > source = v8::String::New(
      "\
      print( 'Hello' );\
      'Hello' + ', World!'\
      "
   );

   // Compile the source code.
   v8::Handle< v8::Script > script = v8::Script::Compile( source );

   // Run the script to get the result.
   v8::Handle< v8::Value > result = script->Run();

   return 0;
}

It does compile fine, but when I run the compiled program I always get an error:

<unknown>:6: Uncaught ReferenceError: print is not defined

What is that I am doing wrong?

2
  • Try changing your script source code to "console.log(this)" and see if print is there. Commented May 19, 2020 at 17:44
  • <unknown>:0: Uncaught ReferenceError: console is not defined Commented May 19, 2020 at 17:49

1 Answer 1

1

What's missing is that you're not doing anything with the global template you set up. If you look at the parameters you can pass to Context::New, you'll find that one can specify an object template for the global object there:

v8::Context::New(isolate, nullptr, global);

You should also set up an Isolate (to pass as isolate there); in fact v8::HandleScope handle_scope; shouldn't even compile without one, at least in the current version.

For more details, see the official documentation, which explains this as well as many other things: https://v8.dev/docs/embed

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

1 Comment

I don't seem to be capable of including libplatform/libplatform.h. I am using ubuntu's libv8-dev package. Should I install somehow else?

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.