Thx Zac, my best bugged code is:
void GetPointX(Local<String> property,
const PropertyCallbackInfo<Value>& info) {
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
int value = static_cast<Point*>(ptr)->x_;
info.GetReturnValue().Set(value);
}
void SetPointX(Local<String> property, Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
static_cast<Point*>(ptr)->x_ = value->Int32Value();
}
void v8_Point(const v8::FunctionCallbackInfo<v8::Value>& args) {
printf("Is constructor call: %s\n", args.IsConstructCall()?"yes":"no");
Point * p = new Point(12);
printf("Internal field count: %d\n",args.This()->InternalFieldCount());
args.This()->SetInternalField(0, External::New(args.GetIsolate(), p));
args.GetReturnValue().Set(args.This());
}
ManagerV8::ManagerV8() {
const char * temp_cc1 = "";
V8::InitializeICUDefaultLocation(temp_cc1);
V8::InitializeExternalStartupData(temp_cc1);
platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
};
ManagerV8::~ManagerV8() {
V8::Dispose();
V8::ShutdownPlatform();
delete platform;
}
std::string ManagerV8::v8_execute(const char* scriptSource, Point*
shareVar) {
Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
std::string temp_variable_result;
Isolate* isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<ObjectTemplate> global_templ = ObjectTemplate::New(isolate);
Local<FunctionTemplate> point_constructor =
FunctionTemplate::New(isolate, v8_Point);
point_constructor->InstanceTemplate()->SetInternalFieldCount(1);
point_constructor->InstanceTemplate()->SetAccessor(String::NewFromUtf8(isolate,
"x"), GetPointX, SetPointX);
Local<Context> context = Context::New(isolate, NULL, global_templ);
Context::Scope context_scope(context);
Local<String> source = String::NewFromUtf8(isolate, scriptSource,
NewStringType::kNormal).ToLocalChecked();
Local<Script> script = Script::Compile(context,
source).ToLocalChecked();
if(!script.IsEmpty()) {
Local<Value> result = script->Run(context).ToLocalChecked();
String::Utf8Value utf8(result);
temp_variable_result = *utf8;
}
}
isolate->Dispose();
delete create_params.array_buffer_allocator;
return temp_variable_result;
}
And the header:
class Point {
public:
Point(int x) : x_(x) { }
int x_;
};
class ManagerV8 {
Platform* platform;
public:
int x_;
ManagerV8();
~ManagerV8();
std::string v8_execute(const char* scriptSource, Point* shareVar);
};
As the result of JS code "*x+1;*" is:
<unknown>:12: Uncaught ReferenceError: x is not defined
#
# Fatal error in v8::ToLocalChecked
# Empty MaybeLocal.
#
Received signal 4 ILL_ILLOPN 000000cf345f
==== C stack trace ===============================
[0x000000cf21ef]
[0x7f06b9b088d0]
[0x000000cf345f]
[0x000002709e28]
[end of stack trace]
All this code based on stackoverflow and Embedder's guide.
Clear code of Embedder's guide don't work at all:
==== C stack trace ===============================
[0x000000cf1dbf]
[0x7fb45a6998d0]
[0x0000004a2851]
[0x00000073959e]
[0x00000078534e]
[0x000000784a2d]
[0x00000072a8b7]
[0x00000072b058]
[0x0000007322fc]
[0x2b4f6cd840dd]
[end of stack trace]
вторник, 25 апреля 2017 г., 6:41:29 UTC+7 пользователь Zac Hansen написал:
>
> it looks at least pretty close. can you post some code and say what
> specific problems you're having?
>
> On Thursday, April 20, 2017 at 12:57:18 AM UTC-7, AleK Shmakov wrote:
>>
>> "Embedder's Guide" <https://github.com/v8/v8/wiki/Embedder%27s-Guide>
>> unfotunatelly is not valid.
>>
>
--
--
v8-users mailing list
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.