Browse Source

Support constructor arguments to JSComponent derived components, while still supporting flat JS components

Josh Engebretson 10 years ago
parent
commit
ac0d2082a3

+ 8 - 0
Source/AtomicJS/Javascript/JSComponent.cpp

@@ -280,6 +280,14 @@ void JSComponent::InitInstance(bool hasArgs, int argIdx)
 
 
         componentFile_->PushModule();
         componentFile_->PushModule();
 
 
+        if (!duk_is_object(ctx, -1))
+        {
+            duk_set_top(ctx, top);
+            return;
+        }
+
+        duk_get_prop_string(ctx, -1, "component");
+
         if (!duk_is_function(ctx, -1))
         if (!duk_is_function(ctx, -1))
         {
         {
             duk_set_top(ctx, top);
             duk_set_top(ctx, top);

+ 13 - 11
Source/AtomicJS/Javascript/JSComponentFile.cpp

@@ -175,21 +175,23 @@ bool JSComponentFile::InitModule()
     if (!PushModule())
     if (!PushModule())
         return false;
         return false;
 
 
-    if (!duk_is_function(ctx, -1))
+    if (duk_is_function(ctx, -1))
     {
     {
-        LOGERRORF("Component file does not export a function: %s", GetName().CString());
-        duk_set_top(ctx, top);
-        return false;
+        // constructor export
+        scriptClass_ = true;
     }
     }
+    else if (duk_is_object(ctx, -1))
+    {
+        duk_get_prop_string(ctx, -1, "component");
 
 
-    // detect a script class vs a simple "flat" javascript component
-    // this means that if a script component class defines a constructor,
-    // it must take 0 arguments (which makes sense as when it is new'd from
-    // serialization, etc there will be no args to pass it
+        if (!duk_is_function(ctx, -1))
+        {
+            LOGERRORF("Component file export object does not export a key \"component\" function: %s", GetName().CString());
+            duk_set_top(ctx, top);
+            return false;
+        }
 
 
-    if (duk_get_length(ctx, -1) == 0)
-    {
-        scriptClass_ = true;
+        scriptClass_ = false;
     }
     }
 
 
     duk_set_top(ctx, top);
     duk_set_top(ctx, top);