TinyJS

Dependencies:   mbed

Fork of TinyJS by Takehisa Oneta

Revision:
9:f80cf055f03d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main2.cpp	Wed Sep 10 08:41:53 2014 +0000
@@ -0,0 +1,80 @@
+#include "mbed.h"
+#include "TinyJS.h"
+#include "TinyJS_Functions.h"
+#include "TinyJS_MathFunctions.h"
+#include "Mbed_Functions.h"
+#include <new>
+
+#define qq(...)  #__VA_ARGS__
+
+static const char* example = qq(
+blinker = function() {
+    var led = 0;
+    mbed.Timeout(function() {
+        led = !led;
+        mbed.DigitalOut('LED1', led);
+        blinker();
+        },
+    0.6);
+};
+blinker();
+);
+
+static const char* example2 = qq(
+function myfunc(x, y) {
+    return x + y;
+}
+print("memfree");
+print(mbed.memfree());
+var a = myfunc(1,2);
+print(a);
+);
+
+
+#if 0 //defined(TARGET_NUCLEO_F401RE)
+static const char* usr_pos = example2;
+#elif defined(TARGET_LPC1768)
+static const char* usr_pos = reinterpret_cast<const char*>(69632);
+#elif defined(TARGET_LPC4088)
+static const char* usr_pos = reinterpret_cast<const char*>(102400);
+#elif defined(TARGET_K64F)
+static const char* usr_pos = reinterpret_cast<const char*>(106496);
+#else
+#error "target error"
+#endif
+
+void js_print(CScriptVar *v, void *userdata); // Script.cpp
+void js_dump(CScriptVar *v, void *userdata);
+extern int mbedErrorFlag; // Script.cpp
+extern std::string mbedErrorMessage; // Script.cpp
+
+static void no_memory () {
+  std::printf("Failed to allocate memory!\n");
+  std::exit (1);
+}
+
+int main() {
+  std::set_new_handler(no_memory);
+  //printf("%s\n", __FILE__);
+
+  CTinyJS *js = new CTinyJS();
+#if defined(TARGET_LPC4088)||defined(TARGET_K64F)   
+  /* add the functions from TinyJS_Functions.cpp */
+  registerFunctions(js);
+  /* add Math and Trigonometry functions from TinyJS_MathFunctions.cpp */
+  registerMathFunctions(js); 
+#endif  
+  /* Add a native function */
+  js->addNative("function print(text)", &js_print, 0);
+  js->addNative("function dump()", &js_dump, js);
+
+  // add mbed functions
+  registerMbedFunctions(js);
+
+  mbedErrorFlag = 0;
+  js->execute(usr_pos);
+  if (mbedErrorFlag != 0) {
+    printf("ERROR: %s\n", mbedErrorMessage.c_str());
+  }
+}
+