I managed to intercept the setting of the instance method with the 
following:

myClassTemplate->PrototypeTemplate()->SetHandler(
NamedPropertyHandlerConfiguration(nullptr, [](Local<Name> property, Local<
Value> value, const PropertyCallbackInfo<Value>& info) {
    printf("This will be called when setting the instance method on the 
prototype");
}));


So the question remains only for the static method interception.


On Monday, March 2, 2020 at 11:10:26 AM UTC+2, Darin Dimitrov wrote:
>
> I am embedding V8 in my application and registered a custom class 
> (MyClass). I am looking for a way to intercept the setting of methods on 
> this class.
>
> Here's my code:
>
>     
> auto platform = platform::NewDefaultPlatform();
> V8::InitializePlatform(platform.get());
> V8::Initialize();
> std::string flags = "--expose_gc --jitless";
> V8::SetFlagsFromString(flags.c_str(), flags.size());
> Isolate::CreateParams create_params;
> create_params.array_buffer_allocator = ArrayBuffer::Allocator::
> NewDefaultAllocator();
>
> Isolate* isolate = Isolate::New(create_params);
> {
>     Isolate::Scope isolate_scope(isolate);
>     HandleScope handle_scope(isolate);
>
>     Local<FunctionTemplate> myClassTemplate = FunctionTemplate::New(
> isolate);
>     Local<FunctionTemplate> myInstanceMethodTemplate = FunctionTemplate::
> New(isolate);
>     myClassTemplate->InstanceTemplate()->Set(v8::String::NewFromUtf8(
> isolate, "myInstanceMethod", v8::NewStringType::kNormal).ToLocalChecked(), 
> myInstanceMethodTemplate);
>
>     Local<ObjectTemplate> globalTemplate = ObjectTemplate::New(isolate);
>     globalTemplate->Set(v8::String::NewFromUtf8(isolate, "MyClass", v8::
> NewStringType::kNormal).ToLocalChecked(), myClassTemplate);
>
>     Local<v8::Context> context = v8::Context::New(isolate, nullptr, 
> globalTemplate);
>     Context::Scope context_scope(context);
>     {
>         Local<v8::Function> myClassCtor = myClassTemplate->GetFunction(
> context).ToLocalChecked();
>         Local<v8::Function> myStaticMethod = v8::Function::New(context, 
> nullptr).ToLocalChecked();
>         assert(myClassCtor->Set(context, v8::String::NewFromUtf8(isolate, 
> "myStaticMethod", v8::NewStringType::kNormal).ToLocalChecked(), 
> myStaticMethod).FromMaybe(false));
>
>         std::string src = R"(
>            // I want to intercept setting this static method and raise 
> some C++ callback when this code is executed
>            MyClass.myStaticMethod = () => { };
>
>            // I want to intercept setting this instance method and raise 
> some C++ callback when this code is executed
>            MyClass.prototype.myInstanceMethod = () => { };
>        )";
>
>        Local<v8::String> source = v8::String::NewFromUtf8(isolate, src.
> c_str(), v8::NewStringType::kNormal).ToLocalChecked();
>        Local<Script> script = Script::Compile(context, source).
> ToLocalChecked();
>        Local<Value> result = script->Run(context).ToLocalChecked();
>        assert(!result.IsEmpty());
>    }
>
> }
>
> isolate->Dispose();
> V8::Dispose();
> V8::ShutdownPlatform();
> delete create_params.array_buffer_allocator;
>
>
>
> In this code I need a C++ callback to be raised when the script is trying 
> to replace some existing static or instance method on my custom class.
>
> Is there some API in V8 that I can use to achieve that?
>
> I have tried registering a NamedPropertyHandlerConfiguration but my 
> callback is never called:
>
> myClassTemplate->InstanceTemplate()->SetHandler(
> NamedPropertyHandlerConfiguration(nullptr, [](Local<Name> property, Local<
> Value> value, const PropertyCallbackInfo<Value>& info) {
>     printf("ok");
> }));
>
>
> Basically I need a way to detect that some script is trying to replace the 
> methods which I have registered manually.
>

-- 
-- 
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/5800a811-3b69-444f-85da-a1516729b41c%40googlegroups.com.

Reply via email to