Browse Source

Static JS functions can now be accessed by both the class contractor as well as instances

Josh Engebretson 10 years ago
parent
commit
38382bb8fa
1 changed files with 10 additions and 2 deletions
  1. 10 2
      Source/ToolCore/JSBind/JavaScript/JSClassWriter.cpp

+ 10 - 2
Source/ToolCore/JSBind/JavaScript/JSClassWriter.cpp

@@ -70,7 +70,11 @@ void JSClassWriter::GenerateSource(String& sourceOut)
 
 void JSClassWriter::GenerateStaticFunctionsSource(String& source, String& packageName)
 {
+    // Store function on both the constructor and prototype
+    // so can access static functions from both the class constructor and an instance
+
     source.AppendWithFormat("js_class_get_constructor(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
+    source.AppendWithFormat("js_class_get_prototype(ctx, \"%s\", \"%s\");\n", packageName.CString(), klass_->GetName().CString());
 
     for (unsigned i = 0; i < klass_->functions_.Size(); i++)
     {
@@ -96,9 +100,13 @@ void JSClassWriter::GenerateStaticFunctionsSource(String& source, String& packag
 
         String scriptName = function->GetName();
         scriptName[0] = tolower(scriptName[0]);
-        source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", scriptName.CString());
+
+        source.Append("duk_dup(ctx, -1);\n");
+        source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
+        source.AppendWithFormat("duk_put_prop_string(ctx, -3, \"%s\");\n", scriptName.CString());
     }
-    source.Append("duk_pop(ctx);\n");
+
+    source.Append("duk_pop_2(ctx);\n");
 }
 
 void JSClassWriter::GenerateNonStaticFunctionsSource(String& source, String& packageName)