2

Is it possible to override an existing's object method in google's V8? For example when String.fromCharCode is called in the java script it will actually call my c++ function MyFromCharCode that will parse and check the arguments and then call the actual String.fromCharCode as if nothing happened. Will this require to define my own String object with it's own methods?

If i do this:

global->Set(v8::String::New("unescape"), v8::FunctionTemplate::New(Unescape));

Then in java script when unescape is called it will call my custom function. ​​​

v8::Handle<v8::Value> Unescape(const v8::Arguments& args) {
    //do some stuff here
    return call_the_real_unescape_from_js(args);
}

Is this possible? And if there is something like this for object methods too (for existing objects like: String, Array).

Or if there is any other way to access a function's/method's arguments when they are called (the functions and methods are standard, like unescape, eval, String.fromCharCode, not custom ones).

Later edit:

Managed to call a standard function:

v8::Handle<v8::Object> global = context->Global();
v8::Handle<v8::Value> value = global->Get(v8::String::New("unescape"));
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
v8::Handle<v8::Value> args[1];
v8::Handle<v8::Value> js_result;
args[0] = v8::String::New("Need%20tips%3F%20Visit%20Schools%21");
js_result = func->Call(global, 1, args);
v8::String::AsciiValue ascii(js_result);
printf("%s\n", ascii);

Still working on how to overwrite and call a method of an existing object (like String.fromChardCode).​​

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Rev Cosmin
  • 41
  • 4
  • Have you tried the code above? – erenon May 19 '14 at 14:23
  • It works to define my own 'unescape' functions. The part where i have to call the real 'unescape' function seems unclear. I have found how to call in c++ a function that is defined in javascript (http://stackoverflow.com/questions/11387015/calling-a-v8-javascript-function-from-c-with-an-argument). But the function i want to call is a standrad already defined one. – Rev Cosmin May 19 '14 at 14:27

0 Answers0