-// -*- mode: c++; c-basic-offset: 4 -*-
/*
- * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
+ * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
#include "config.h"
#include "JSCallbackConstructor.h"
+#include "APIShims.h"
#include "APICast.h"
-#include <kjs/JSGlobalObject.h>
-#include <kjs/object_object.h>
+#include <runtime/Error.h>
+#include <runtime/JSGlobalObject.h>
+#include <runtime/JSLock.h>
+#include <runtime/ObjectPrototype.h>
#include <wtf/Vector.h>
-namespace KJS {
+namespace JSC {
-const ClassInfo JSCallbackConstructor::info = { "CallbackConstructor", 0, 0};
+const ClassInfo JSCallbackConstructor::s_info = { "CallbackConstructor", &JSNonFinalObject::s_info, 0, 0, CREATE_METHOD_TABLE(JSCallbackConstructor) };
-JSCallbackConstructor::JSCallbackConstructor(ExecState* exec, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
- : JSObject(exec->lexicalGlobalObject()->objectPrototype())
+JSCallbackConstructor::JSCallbackConstructor(JSGlobalObject* globalObject, Structure* structure, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
+ : JSNonFinalObject(globalObject->globalData(), structure)
, m_class(jsClass)
, m_callback(callback)
{
+}
+
+void JSCallbackConstructor::finishCreation(JSGlobalObject* globalObject, JSClassRef jsClass)
+{
+ Base::finishCreation(globalObject->globalData());
+ ASSERT(inherits(&s_info));
if (m_class)
JSClassRetain(jsClass);
}
JSClassRelease(m_class);
}
-bool JSCallbackConstructor::implementsHasInstance() const
-{
- return true;
-}
-
-bool JSCallbackConstructor::implementsConstruct() const
+void JSCallbackConstructor::destroy(JSCell* cell)
{
- return true;
+ jsCast<JSCallbackConstructor*>(cell)->JSCallbackConstructor::~JSCallbackConstructor();
}
-JSObject* JSCallbackConstructor::construct(ExecState* exec, const List &args)
+static EncodedJSValue JSC_HOST_CALL constructJSCallback(ExecState* exec)
{
+ JSObject* constructor = exec->callee();
JSContextRef ctx = toRef(exec);
- JSObjectRef thisRef = toRef(this);
+ JSObjectRef constructorRef = toRef(constructor);
- if (m_callback) {
- int argumentCount = static_cast<int>(args.size());
+ JSObjectCallAsConstructorCallback callback = jsCast<JSCallbackConstructor*>(constructor)->callback();
+ if (callback) {
+ int argumentCount = static_cast<int>(exec->argumentCount());
Vector<JSValueRef, 16> arguments(argumentCount);
for (int i = 0; i < argumentCount; i++)
- arguments[i] = toRef(args[i]);
-
- JSLock::DropAllLocks dropAllLocks;
- return toJS(m_callback(ctx, thisRef, argumentCount, arguments.data(), toRef(exec->exceptionSlot())));
+ arguments[i] = toRef(exec, exec->argument(i));
+
+ JSValueRef exception = 0;
+ JSObjectRef result;
+ {
+ APICallbackShim callbackShim(exec);
+ result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
+ }
+ if (exception)
+ throwError(exec, toJS(exec, exception));
+ // result must be a valid JSValue.
+ if (!result)
+ return throwVMTypeError(exec);
+ return JSValue::encode(toJS(result));
}
- return toJS(JSObjectMake(ctx, m_class, 0));
+ return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
+}
+
+ConstructType JSCallbackConstructor::getConstructData(JSCell*, ConstructData& constructData)
+{
+ constructData.native.function = constructJSCallback;
+ return ConstructTypeHost;
}
-} // namespace KJS
+} // namespace JSC