]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - API/JSCallbackConstructor.cpp
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / API / JSCallbackConstructor.cpp
index 46c1823d21e2cad982ab699ebe4d09d055853015..fa9d216dd11250127ee685fe1ec8d59ff268c99e 100644 (file)
@@ -1,6 +1,5 @@
-// -*- 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
  *
  * 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 "config.h"
 #include "JSCallbackConstructor.h"
 
+#include "APIShims.h"
 #include "APICast.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>
 
 #include <wtf/Vector.h>
 
-namespace KJS {
+namespace JSC {
 
 
-const ClassInfo JSCallbackConstructor::info = { "CallbackConstructor", 0, 0};
+const ClassInfo JSCallbackConstructor::s_info = { "CallbackConstructor", &JSObjectWithGlobalObject::s_info, 0, 0 };
 
 
-JSCallbackConstructor::JSCallbackConstructor(ExecState* exec, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
-    : JSObject(exec->lexicalGlobalObject()->objectPrototype())
+JSCallbackConstructor::JSCallbackConstructor(JSGlobalObject* globalObject, Structure* structure, JSClassRef jsClass, JSObjectCallAsConstructorCallback callback)
+    : JSObjectWithGlobalObject(globalObject, structure)
     , m_class(jsClass)
     , m_callback(callback)
 {
     , m_class(jsClass)
     , m_callback(callback)
 {
+    ASSERT(inherits(&s_info));
     if (m_class)
         JSClassRetain(jsClass);
 }
     if (m_class)
         JSClassRetain(jsClass);
 }
@@ -51,32 +54,37 @@ JSCallbackConstructor::~JSCallbackConstructor()
         JSClassRelease(m_class);
 }
 
         JSClassRelease(m_class);
 }
 
-bool JSCallbackConstructor::implementsHasInstance() const
-{
-    return true;
-}
-
-bool JSCallbackConstructor::implementsConstruct() const
-{
-    return true;
-}
-
-JSObject* JSCallbackConstructor::construct(ExecState* exec, const List &args)
+static EncodedJSValue JSC_HOST_CALL constructJSCallback(ExecState* exec)
 {
 {
+    JSObject* constructor = exec->callee();
     JSContextRef ctx = toRef(exec);
     JSContextRef ctx = toRef(exec);
-    JSObjectRef thisRef = toRef(this);
+    JSObjectRef constructorRef = toRef(constructor);
 
 
-    if (m_callback) {
-        int argumentCount = static_cast<int>(args.size());
+    JSObjectCallAsConstructorCallback callback = static_cast<JSCallbackConstructor*>(constructor)->callback();
+    if (callback) {
+        int argumentCount = static_cast<int>(exec->argumentCount());
         Vector<JSValueRef, 16> arguments(argumentCount);
         for (int i = 0; i < argumentCount; i++)
         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));
+        return JSValue::encode(toJS(result));
     }
     
     }
     
-    return toJS(JSObjectMake(ctx, m_class, 0));
+    return JSValue::encode(toJS(JSObjectMake(ctx, static_cast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
+}
+
+ConstructType JSCallbackConstructor::getConstructData(ConstructData& constructData)
+{
+    constructData.native.function = constructJSCallback;
+    return ConstructTypeHost;
 }
 
 }
 
-} // namespace KJS
+} // namespace JSC