Thanks your reply Ben,
   
   I have tried as you suggested :

  context->Global()->SetInternalField(0, obj);

   but didn't worked ...

 do i am doing anything wrong during the initialization ?

On Monday, August 19, 2019 at 11:30:57 PM UTC+5:30, Ben Noordhuis wrote:
>
> On Mon, Aug 19, 2019 at 5:19 PM Sudheer Kumar <kumsu...@gmail.com 
> <javascript:>> wrote: 
> > 
> > Hello everyone, 
> >    I am struggling wrapping and unwrapping the c++ object (MYCLASS) . 
> below is the sample code i have written for my exact requirement: 
> > 
> > 
> >   void Update_Rotation(const v8::FunctionCallbackInfo<v8::Value>& info); 
> > 
> > class MYCLASS { 
> >  public: 
> > 
> >   Point(D_TYPE1 *par1) : rotate(par1) { } 
> > 
> >   D_TYPE1 *rotate; 
> > 
> >   void run_script() 
> >   { 
> >     v8::V8::InitializeICUDefaultLocation(argv); 
> >     v8::V8::InitializeExternalStartupData(argv); 
> >     std::unique_ptr<v8::Platform> platform = 
> v8::platform::NewDefaultPlatform(); 
> >     v8::V8::InitializePlatform(platform.get()); 
> >     v8::V8::Initialize(); 
> > 
> >     v8::Isolate::CreateParams create_params; 
> >     create_params.array_buffer_allocator = 
> v8::ArrayBuffer::Allocator::NewDefaultAllocator(); 
> >     v8::Isolate* isolate = v8::Isolate::New(create_params); 
> >     v8::Isolate::Scope isolate_scope(isolate); 
> > 
> > v8::Isolate::Scope isolate_scope(isolate); 
> > 
> > // Create a stack-allocated handle scope. 
> > v8::HandleScope handle_scope(isolate); 
> > 
> > // Create a template for the global object. 
> > v8::Handle<v8::ObjectTemplate> global = 
> v8::ObjectTemplate::New(isolate); 
> > 
> > /* Here the internal field count is set to 1 which means the object has 
> one internal field, with an index of 0, that points to a C++ object.*/ 
> > global->SetInternalFieldCount(1); 
> > 
> > // Bind the 'update_rotation' function 
> > global->Set(v8::String::NewFromUtf8(isolate, "update_rotation", 
> v8::NewStringType::kNormal).ToLocalChecked(), 
> > v8::FunctionTemplate::New(isolate, Update_Rotation)); 
> > // Create a new context. 
> > v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, 
> global); 
> > //Persistent<Context> context = v8::Context::New(isolate, NULL, global, 
> nullptr); 
> > 
> > // Enter the context for compiling and running the script. 
> > v8::Context::Scope context_scope(context); 
> > 
> > Local<Object> obj = global->NewInstance(context).ToLocalChecked(); 
> > obj->SetInternalField(0, External::New(isolate, this)); 
> > /* My JS script */ 
> > v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, "var 
> rotate_status = 
> update_rotation();",v8::NewStringType::kNormal).ToLocalChecked(); 
> > //exception handler 
> > v8::TryCatch try_catch(isolate); 
> > 
> > printf("Script : compilation started\n"); 
> > // Compile the source code. 
> > v8::Local<v8::Script> script = v8::Script::Compile(context, 
> source).ToLocalChecked(); 
> > printf("Script : compilation compilation done\n"); 
> > printf("Script : Running started\n"); 
> > // Run the script to get the result. 
> > v8::Local<v8::Value> result = script->Run(context).ToLocalChecked(); 
> >   } 
> > }; 
> > 
> > void Update_Rotation(const v8::FunctionCallbackInfo<v8::Value>& info) { 
> > Local<Object> self = info.Holder(); 
> > Local<External> wrap = Local<External>::Cast(self->GetInternalField(0)); 
> > void* ptr = wrap->Value(); 
> > MYCLASS* temp_sp = static_cast<MYCLASS*>(ptr);                           
> //--> Here i am always getting null pointer 
> > D_TYPE1 *rotate_ptr = static_cast<Point*>(ptr)->rotate; 
> > 
> >     rotate_ptr->Update(); 
> > } 
> > 
> > 
> > int main() 
> > { 
> >     D_TYPE1 *sample_rotate_obj = new D_TYPE1(); 
> >     MYCLASS test_obj(sample_rotate_obj); 
> > 
> >     test_obj.run_script(); 
> > } 
> > 
> > 
> > Can anyone plz suggest how to fix it to get valid pointer to my object? 
> > 
> > Thanks in advance ... 
> > 
> > Regards 
> > Sudheer 
>
> To summarize what your code does: 
>
> 1. It creates an ObjectTemplate for the global template. 
> 2. It passes that ObjectTemplate to Context::New() 
> 3. It then creates a new instance of the same ObjectTemplate and sets 
> its internal field #0. 
>
> In other words, two instances are created: once by you, once 
> (implicitly) by Context::New(). 
>
> The one you created manually isn't used. When JS code calls your 
> update_rotation() method, it does so using the instance that 
> Context::New() created. 
>
> I didn't test but calling context->Global()->SetInternalField() might 
> fix your issue. 
>

-- 
-- 
v8-users mailing list
v8-users@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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/16a0d7d7-ed42-4bca-a841-8d13013626e8%40googlegroups.com.

Reply via email to