فهرست منبع

Detect script class component vs simple flat component based on argument count on exported function

Josh Engebretson 10 سال پیش
والد
کامیت
2fa7f62763
2فایلهای تغییر یافته به همراه10 افزوده شده و 8 حذف شده
  1. 0 1
      Source/AtomicJS/Javascript/JSComponent.cpp
  2. 10 7
      Source/AtomicJS/Javascript/JSComponentFile.cpp

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

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

+ 10 - 7
Source/AtomicJS/Javascript/JSComponentFile.cpp

@@ -172,18 +172,21 @@ bool JSComponentFile::InitModule()
     if (!PushModule())
         return false;
 
-    if (duk_is_function(ctx, -1))
+    if (!duk_is_function(ctx, -1))
     {
-        // TODO: verify JSComponent in prototype chain
-        scriptClass_ = true;
+        LOGERRORF("Component file does not export a function: %s", GetName().CString());
         duk_set_top(ctx, top);
-        return true;
+        return false;
     }
 
-    if (!duk_is_object(ctx, -1))
+    // 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_get_length(ctx, -1) == 0)
     {
-        duk_set_top(ctx, top);
-        return false;
+        scriptClass_ = true;
     }
 
     duk_set_top(ctx, top);