| 74 | |
| 75 | template <typename T> |
| 76 | EncodedJSValue JSC_HOST_CALL APICallbackFunction::construct(ExecState* exec) |
| 77 | { |
| 78 | VM& vm = exec->vm(); |
| 79 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 80 | JSObject* constructor = exec->jsCallee(); |
| 81 | JSContextRef ctx = toRef(exec); |
| 82 | JSObjectRef constructorRef = toRef(constructor); |
| 83 | |
| 84 | JSObjectCallAsConstructorCallback callback = jsCast<T*>(constructor)->constructCallback(); |
| 85 | if (callback) { |
| 86 | size_t argumentCount = exec->argumentCount(); |
| 87 | Vector<JSValueRef, 16> arguments; |
| 88 | arguments.reserveInitialCapacity(argumentCount); |
| 89 | for (size_t i = 0; i < argumentCount; ++i) |
| 90 | arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i))); |
| 91 | |
| 92 | JSValueRef exception = 0; |
| 93 | JSObjectRef result; |
| 94 | { |
| 95 | JSLock::DropAllLocks dropAllLocks(exec); |
| 96 | result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception); |
| 97 | } |
| 98 | if (exception) { |
| 99 | throwException(exec, scope, toJS(exec, exception)); |
| 100 | return JSValue::encode(toJS(exec, exception)); |
| 101 | } |
| 102 | // result must be a valid JSValue. |
| 103 | if (!result) |
| 104 | return throwVMTypeError(exec, scope); |
| 105 | return JSValue::encode(toJS(result)); |
| 106 | } |
| 107 | |
| 108 | return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0))); |
| 109 | } |
| 110 | |
| 111 | } // namespace JSC |
| 112 | |