Explorar el Código

Update jerryscript and add console class

Bernard Xiong hace 8 años
padre
commit
0bf45a612e
Se han modificado 5 ficheros con 102 adiciones y 4 borrados
  1. 1 1
      jerryscript
  2. 34 0
      rtthread-port/jerry_console.c
  3. 51 3
      rtthread-port/jerry_util.c
  4. 7 0
      rtthread-port/jerry_util.h
  5. 9 0
      rtthread-port/port.c

+ 1 - 1
jerryscript

@@ -1 +1 @@
-Subproject commit e8608707b6d9486022b4d72b280303923945fdeb
+Subproject commit 3c57698ed8a80c276b163390721aa1269fb75169

+ 34 - 0
rtthread-port/jerry_console.c

@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <rtthread.h>
+
+#include "jerry_util.h"
+
+DECLARE_HANDLER(dir)
+{
+    size_t index;
+    for (index = 0; index < args_cnt; index ++)
+    {
+        js_value_dump(args[index]);
+    }
+
+    return jerry_create_undefined();
+}
+
+int js_console_init()
+{
+    jerry_value_t console = jerry_create_object();
+    jerry_value_t global_obj = jerry_get_global_object();
+
+    REGISTER_METHOD_ALIAS(console, log, jerryx_handler_print);
+    REGISTER_METHOD_ALIAS(console, dir, dir_handler);
+
+    js_set_property(global_obj, (jerry_char_t *)"console", console);
+
+    jerry_release_value(global_obj);
+    jerry_release_value(console);
+
+    return 0;
+}

+ 51 - 3
rtthread-port/jerry_util.c

@@ -82,8 +82,8 @@ bool object_dump_foreach(const jerry_value_t property_name,
 
     first_property = (int *)user_data_p;
 
-    // if (*first_property) first_property = 0;
-    // else
+    if (*first_property) *first_property = 0;
+    else
     {
         printf(",");
     }
@@ -142,7 +142,7 @@ void js_value_dump(jerry_value_t value)
     }
     else if (jerry_value_is_function(value))
     {
-        printf("function");
+        printf("[function]");
     }
     else if (jerry_value_is_constructor(value))
     {
@@ -172,3 +172,51 @@ void js_value_dump(jerry_value_t value)
         printf("what?");
     }
 }
+
+int js_read_file(const char* filename, char **script)
+{
+    FILE *fp;
+    int length = 0;
+
+    if (!filename || !script) return 0;
+
+    fp = fopen(filename, "rb");
+    if (fp)
+    {
+        fseek(fp, 0, SEEK_END);
+        length = ftell(fp);
+        fseek(fp, 0, SEEK_SET);
+
+        if (length)
+        {
+            char *script_str = (char*) malloc (length + 1);
+            if (script_str)
+            {
+                script_str[length] = '\0';
+                if (fread(script_str, length, 1, fp) == 1)
+                {
+                    *script = script_str;
+                }
+                else
+                {
+                    printf("read failed!\n");
+                }
+            }
+            else length = 0;
+        }
+        fclose(fp);
+    }
+
+    return length;
+}
+
+extern int js_console_init();
+extern int js_module_init();
+
+int js_util_init(void)
+{
+    js_console_init();
+    // js_module_init();
+
+    return 0;
+}

+ 7 - 0
rtthread-port/jerry_util.h

@@ -12,8 +12,12 @@
 
 #define REGISTER_HANDLER(NAME) \
     jerryx_handler_register_global ( (jerry_char_t *)# NAME, NAME ## _handler)
+#define REGISTER_HANDLER_ALIAS(NAME, HANDLER) \
+    jerryx_handler_register_global ( (jerry_char_t *)# NAME, HANDLER)
 #define REGISTER_METHOD(OBJ, NAME) \
     js_add_function (OBJ, # NAME, NAME ## _handler)
+#define REGISTER_METHOD_ALIAS(OBJ, NAME, HANDLER) \
+    js_add_function (OBJ, # NAME, HANDLER)
 
 #ifdef __cplusplus
 extern "C"
@@ -33,6 +37,9 @@ jerry_value_t js_call_function(const jerry_value_t obj, const char *name,
 char *js_value_to_string(const jerry_value_t value);
 
 void js_value_dump(const jerry_value_t value);
+int js_read_file(const char* filename, char **script);
+
+int js_util_init(void);
 
 #ifdef __cplusplus
 }

+ 9 - 0
rtthread-port/port.c

@@ -82,6 +82,15 @@ void jerry_port_log (jerry_log_level_t level, const char *format, ...)
     va_end(args);
 }
 
+/**
+ * Default implementation of jerryx_port_handler_print_char. Uses 'printf' to
+ * print a single character to standard output.
+ */
+void jerryx_port_handler_print_char (char c) /**< the character to print */
+{
+    rt_kprintf("%c", c);
+} /* jerryx_port_handler_print_char */
+
 /*
  * Date Port API
  */