pmed / v8pp

Bind C++ functions and classes into V8 JavaScript engine
http://pmed.github.io/v8pp/
Other
887 stars 119 forks source link

Newer API in V8 version 7.6 #131

Closed pmed closed 3 years ago

pmed commented 4 years ago

For example for v8::String::NewFromUtf8()

lemmel commented 4 years ago

Hi !

I'm discovering your project (with no prior experience with the v8 engine) and I was making some wrapping when I tested something like this:

int f(int x) { return x * 2; }

v8::Isolate* isolate = v8::Isolate::GetCurrent();

v8::Local<v8::Function> v8_fun = v8pp::wrap_function(isolate, "f", &f);
isolate->GetCurrentContext()-> Global()->Set(v8pp::to_v8(isolate, "v8_fun"), v8_fun);

That is coming from the documentation: https://github.com/pmed/v8pp/blob/master/docs/wrapping.md

The problem is that the Set methods signatures are:

  V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context,
                                        Local<Value> key, Local<Value> value);

  V8_WARN_UNUSED_RESULT Maybe<bool> Set(Local<Context> context, uint32_t index,
                                        Local<Value> value);

A context is required and I can't achieve to access to the context, and thus give it to v8.

Is that a result of the new version of v8 API, or am I missing something ?

pmed commented 4 years ago

Hi,

the documentation seems to be outdated, because of the changes in V8 API. That deprecated method v8::Object::Set(key, value) was removed in the newer V8 releases. So you could try to use the newer one, with the additional context argument.

There is at least one context in a V8 Isolate, that could be accessed with a function named literally GetCurrentContext(). I would try to use it.