Browse Source

Update to NativePluginExample

JoshEngebretson 10 years ago
parent
commit
9ff820ed1b

+ 28 - 6
NativePluginExample/Native/MyNativePlugin.cpp

@@ -1,29 +1,51 @@
 
 
+// ATOMIC_PLUGIN_MAIN must be defined in one (and only one) plugin source file
+// before including AtomicPlugin.h
 #define ATOMIC_PLUGIN_MAIN
 #define ATOMIC_PLUGIN_MAIN
+
 #include "AtomicPlugin.h"
 #include "AtomicPlugin.h"
 
 
+// define C linkage so that we can easily get functions from shared library 
 extern "C"
 extern "C"
 {
 {
+    // a cfunction which returns the answer to life, the universe, and everything 
     static int js_getAnswer(duk_context* ctx)
     static int js_getAnswer(duk_context* ctx)
     {
     {
         duk_push_number(ctx, 42);
         duk_push_number(ctx, 42);
         return 1;
         return 1;
+    }
+
+    // a cfunction which checks that the answer is correct
+    static int js_checkAnswer(duk_context* ctx)
+    {
+        int answer = duk_require_int(ctx, 0);
 
 
+        answer == 42 ? duk_push_true(ctx) : duk_push_false(ctx);
+
+        return 1;
     }
     }
 
 
-    // plugin init is a duk cfunction itself, so we can pcall the import	
+    // the function list that out native plugin exports
+    static const duk_function_list_entry plugin_funcs[] = {
+        { "getAnswer", js_getAnswer, 0 /*nargs*/ },
+        { "checkAnswer", js_checkAnswer, 1 /*nargs*/ },
+        { NULL, NULL, 0 }
+    };
+
+    // main plugin initialization function, which is a standard Duktape cfunction
+    // must use PLUGIN_EXPORT_API for function to be exported from dll on Windows
     int PLUGIN_EXPORT_API atomic_plugin_init(duk_context* ctx)
     int PLUGIN_EXPORT_API atomic_plugin_init(duk_context* ctx)
     {
     {
-        // exports object at 0
-        if (!duk_is_object(ctx, 0))
+        // modules's exports object should be at index 0
+        if (!duk_get_top(ctx) || !duk_is_object(ctx, 0))
         {
         {
+            // not an object, something went wrong
             duk_push_boolean(ctx, 0);
             duk_push_boolean(ctx, 0);
             return 1;
             return 1;
         }
         }
 
 
-        // export out native functions
-        duk_push_c_function(ctx, js_getAnswer, 0);
-        duk_put_prop_string(ctx, -2, "getAnswer");
+        // export our native functions
+        duk_put_function_list(ctx, 0, plugin_funcs);
 
 
         // and return success
         // and return success
         duk_push_boolean(ctx, 1);
         duk_push_boolean(ctx, 1);

+ 2 - 2
NativePluginExample/Resources/Scripts/main.js

@@ -11,8 +11,8 @@ Atomic.game.init(start, update);
 function start() {
 function start() {
 
 
 	// call our native method
 	// call our native method
-	print("The answer is: ", NativePlugin.getAnswer());
-
+	var answer = NativePlugin.getAnswer();
+	print("The answer is: ", answer, " which is " , NativePlugin.checkAnswer(answer) ? "correct" : "incorrect"); 
 	var game = Atomic.game;
 	var game = Atomic.game;
 
 
 	// create a 2D scene
 	// create a 2D scene