Skip to content

Commit 9ba8b5d

Browse files
Kent Hansennierob
authored andcommitted
v8: Add Api to call objects
1 parent 9408957 commit 9ba8b5d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/3rdparty/v8/include/v8.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,20 @@ class Object : public Value {
16981698

16991699
V8EXPORT static Local<Object> New();
17001700
static inline Object* Cast(Value* obj);
1701+
1702+
#ifdef QT_BUILD_SCRIPT_LIB
1703+
/**
1704+
* Returns wether the object can be called as a function
1705+
*/
1706+
V8EXPORT bool IsCallable();
1707+
/**
1708+
* Call the object as a function
1709+
*/
1710+
V8EXPORT Local<Value> Call(Handle<Object> recv,
1711+
int argc,
1712+
Handle<Value> argv[]);
1713+
#endif
1714+
17011715
private:
17021716
V8EXPORT Object();
17031717
V8EXPORT static void CheckCast(Value* obj);

src/3rdparty/v8/src/api.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,49 @@ int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
32133213
}
32143214
}
32153215

3216+
#ifdef QT_BUILD_SCRIPT_LIB
3217+
bool v8::Object::IsCallable() {
3218+
i::Isolate* isolate = i::Isolate::Current();
3219+
ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false);
3220+
ENTER_V8(isolate);
3221+
i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3222+
if (obj->IsJSFunction())
3223+
return true;
3224+
HandleScope scope;
3225+
return i::Execution::GetFunctionDelegate(obj)->IsJSFunction();
3226+
}
3227+
3228+
Local<v8::Value> v8::Object::Call(v8::Handle<v8::Object> recv, int argc,
3229+
v8::Handle<v8::Value> argv[]) {
3230+
i::Isolate* isolate = i::Isolate::Current();
3231+
ON_BAILOUT(isolate, "v8::Object::Call()", return Local<v8::Value>());
3232+
LOG_API(isolate, "Object::Call");
3233+
ENTER_V8(isolate);
3234+
i::Object* raw_result = NULL;
3235+
{
3236+
HandleScope scope;
3237+
i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
3238+
i::Handle<i::Object> recv_obj = Utils::OpenHandle(*recv);
3239+
STATIC_ASSERT(sizeof(v8::Handle<v8::Value>) == sizeof(i::Object**));
3240+
i::Object*** args = reinterpret_cast<i::Object***>(argv);
3241+
i::Handle<i::JSFunction> fun;
3242+
if (obj->IsJSFunction()) {
3243+
fun = i::Handle<i::JSFunction>::cast(obj);
3244+
} else {
3245+
fun = i::Handle<i::JSFunction>::cast(i::Execution::GetFunctionDelegate(obj));
3246+
recv_obj = obj;
3247+
}
3248+
EXCEPTION_PREAMBLE(isolate);
3249+
i::Handle<i::Object> returned =
3250+
i::Execution::Call(fun, recv_obj, argc, args, &has_pending_exception);
3251+
EXCEPTION_BAILOUT_CHECK(isolate, Local<Object>());
3252+
raw_result = *returned;
3253+
}
3254+
i::Handle<i::Object> result(raw_result);
3255+
return Utils::ToLocal(result);
3256+
}
3257+
#endif
3258+
32163259

32173260
Local<v8::Object> Function::NewInstance() const {
32183261
return NewInstance(0, NULL);

0 commit comments

Comments
 (0)