Skip to content

Commit

Permalink
add N-API support & optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
qile222 committed Nov 9, 2019
1 parent d78be51 commit 5ce35e5
Show file tree
Hide file tree
Showing 59 changed files with 1,959 additions and 1,283 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
.vscode/
build/
build-freertos/
build-*/
.idea/
sdkconfig
sdkconfig.old
.DS_Store
js-snapshots.h
js-snapshots.c
rtnode-snapshots.h
rtnode-snapshots.c
*.pyc
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ The following runtime modules are built in:
- utils
- console
- assert
- N-API

In order to be compatible with different embed JavaScript engines, N-API will be supported in the future.
N-API is supported in order to be compatible with different embed JavaScript engines, the following N-API features is WIP:
- async
- thread safe function

## Dependencies

Expand All @@ -20,7 +23,7 @@ In order to be compatible with different embed JavaScript engines, N-API will be

## Example

JavaScript sources are packaged in `src/js-snapshots.c/h`. Use `sh tools/js2c.sh` to package JavaScript Sources if the sources is changed.
JavaScript sources are packaged in `src/js-snapshots.c/h`. Use `sh tools/js2c.sh` to package JavaScript Sources if the sources are changed.

`src/js/app.js` is the entry of JavaScript, here is an example:

Expand Down
2 changes: 1 addition & 1 deletion deps/jerryscript/jerry-debugger/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Available JerryScript debugger tools

- JerryScript console debugger client ( jerry_client.py )
- IoT.js Code ( https://github.com/jerryscript-project/iotjscode )
- IoT.js Code ( https://github.com/jerryscript-project/rtnodecode )
- JerryScript debugger Chrome webtool ( https://github.com/jerryscript-project/jerryscript-debugger-ts )
10 changes: 2 additions & 8 deletions include/node_api.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Pulled from nodejs/node#daff0be5b31f769357d943f9694f866b7d286647 v10.13.0 using tools/pull-napi.sh
// Pulled from nodejs/node#8742cbfef0d31d7fad49ced7d11e6827b932b101 v10.8.0 using tools/pull-napi.sh

#ifndef SRC_NODE_API_H_
#define SRC_NODE_API_H_
Expand Down Expand Up @@ -600,7 +600,7 @@ NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
int64_t change_in_bytes,
int64_t* adjusted_value);

// Running a script
// Runnig a script
NAPI_EXTERN napi_status napi_run_script(napi_env env,
napi_value script,
napi_value* result);
Expand Down Expand Up @@ -697,12 +697,6 @@ NAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env,
int* sign_bit,
size_t* word_count,
uint64_t* words);
NAPI_EXTERN napi_status napi_add_finalizer(napi_env env,
napi_value js_object,
void* native_object,
napi_finalize finalize_cb,
void* finalize_hint,
napi_ref* result);
#endif // NAPI_EXPERIMENTAL

EXTERN_C_END
Expand Down
2 changes: 1 addition & 1 deletion include/node_api_types.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Pulled from nodejs/node#daff0be5b31f769357d943f9694f866b7d286647 v10.13.0 using tools/pull-napi.sh
// Pulled from nodejs/node#8742cbfef0d31d7fad49ced7d11e6827b932b101 v10.8.0 using tools/pull-napi.sh

#ifndef SRC_NODE_API_TYPES_H_
#define SRC_NODE_API_TYPES_H_
Expand Down
94 changes: 94 additions & 0 deletions include/rt-node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#ifndef _JS_MAIN_H_
#define _JS_MAIN_H_

#include "rtev.h"
#include "rtnode-binding.h"
#include "rtnode-error.h"
#include "rtnode-logger.h"

#define NODE_MAJOR_VERSION 0
#define NODE_MINOR_VERSION 0
#define NODE_PATCH_VERSION 1

#if !defined(STRINGIFY)
#define STRINGIFY(x) #x
#endif /* STRINGIFY */

#if !defined(TOSTRING)
#define TOSTRING(x) STRINGIFY(x)
#endif /* TOSTRING */

#define RTNODE_ABORT() \
do { RTNODE_LOG_E("abort message: %s:%d", __FILE__, __LINE__); abort(); } while (false)

#define RTNODE_ASSERT(exp) \
do { \
if(!(exp)) { \
RTNODE_LOG_E("assert message: %s", #exp); \
assert(0); \
} \
} while (false)

#define RTNODE_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(name) \
static void rtnode_##name##_destroy(rtnode_##name##_t* wrap); \
static const jerry_object_native_info_t this_module_native_info = { \
.free_cb = (jerry_object_native_free_callback_t)rtnode_##name##_destroy \
}

#define RTNODE_CHECK_FATAL_ERROR(jobj, source) \
if (jerry_value_is_error(jobj)) { \
rtnode_on_fatal_error(jobj, source); \
assert(0); \
}

#define RTNODE_CREATE_ERROR(TYPE, message) \
jerry_create_error(JERRY_ERROR_##TYPE, (const jerry_char_t*)message)

#define RTNODE_DECLARE_PTR(JOBJ, TYPE, NAME) \
TYPE* NAME = NULL; \
do { \
if (!jerry_get_object_native_pointer(JOBJ, (void**)&NAME, \
&native_info)) { \
return RTNODE_CREATE_ERROR(COMMON, "Internal"); \
} \
} while (0)

#define RTNODE_FUNCTION(name) \
static jerry_value_t name(const jerry_value_t jfunc, \
const jerry_value_t jthis, \
const jerry_value_t jargv[], \
const jerry_length_t jargc)

#ifndef RTNODE_VM_HEAP_SIZE
#define RTNODE_VM_HEAP_SIZE 128 * 1024
#endif

#define RTNODE_BACKTRACE_DEPTH 11

/* Avoid compiler warnings if needed. */
#define RTNODE_UNUSED(x) ((void)(x))

void rtnode_free(void *ptr);

void* rtnode_malloc(size_t size);

void* rtnode_realloc(void *ptr, size_t size);

void* rtnode_calloc(size_t count, size_t size);

void* rtnode_jerry_alloc(size_t size, void *cb_data);

uint64_t rtnode_get_memory_total();

uint64_t rtnode_get_memory_alloc_count();

typedef struct {
rtev_ctx_t *rtev;
jerry_context_t* jerry;
} rtnode_context_t;

extern rtnode_context_t *js_ctx;

extern int rtnode_start();

#endif //_JS_MAIN_H_
6 changes: 0 additions & 6 deletions include/rt_node.h

This file was deleted.

88 changes: 49 additions & 39 deletions platforms/esp-idf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,40 @@ set(COMPONENT_ADD_INCLUDEDIRS
../../src
../../src/modules
../../include
)
)
set(COMPONENT_SRCS
main.c
../../src/js-binding.c
../../src/js-error.c
../../src/js-logger.c
../../src/js-main.c
../../src/js-modules.c
../../src/js-native.c
../../src/js-snapshots.c
../../src/js-common.c
../../src/js-rtev-watcher.c
../../src/modules/js-module-console.c
../../src/modules/js-module-process.c
../../src/modules/js-module-require.c
../../src/modules/js-module-timer.c
../../src/rtnode.c
../../src/rtnode-binding.c
../../src/rtnode-error.c
../../src/rtnode-logger.c
../../src/rtnode-modules.c
../../src/rtnode-native.c
../../src/rtnode-common.c
../../src/rtnode-rtev-watcher.c
../../src/rtnode-snapshots.c
../../src/modules/rtnode-module-console.c
../../src/modules/rtnode-module-process.c
../../src/modules/rtnode-module-require.c
../../src/modules/rtnode-module-timer.c
../../src/napi/node_api.c
# ../../src/napi/node_api_async.c
../../src/napi/node_api_env.c
../../src/napi/node_api_function.c
../../src/napi/node_api_lifetime.c
../../src/napi/node_api_module.c
../../src/napi/node_api_object_wrap.c
../../src/napi/node_api_property.c
# ../../src/napi/node_api_tsfn.c
../../src/napi/node_api_value.c
../../deps/rtev/src/allocator.c
../../deps/rtev/src/async.c
../../deps/rtev/src/context.c
../../deps/rtev/src/threadpool.c
../../deps/rtev/src/tick.c
../../deps/rtev/src/timer.c
../../deps/rtev/src/watcher.c
)
)

register_component()

Expand All @@ -48,32 +58,32 @@ externalproject_add(jerryscript_build
INSTALL_COMMAND "" # Do not install to host
LIST_SEPARATOR | # Use the alternate list separator
CMAKE_ARGS
-DJERRY_GLOBAL_HEAP_SIZE=${JERRY_GLOBAL_HEAP_SIZE}
-DJERRY_CMDLINE=OFF
-DJERRY_ERROR_MESSAGES=ON
-DJERRY_LINE_INFO=ON
-DJERRY_EXTERNAL_CONTEXT=ON
-DJERRY_SNAPSHOT_EXEC=ON
-DJERRY_SNAPSHOT_SAVE=ON
-DJERRY_PARSER=ON
-DENABLE_LTO=OFF # FIXME: This option must be turned off or the cross-compiler settings will be overwritten
-DCMAKE_C_COMPILER_WORKS=true # cross-compiler
-DCMAKE_SYSTEM_NAME=Generic
-DCMAKE_SYSTEM_PROCESSOR=xtensa
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DEXTERNAL_COMPILE_FLAGS=${EXTERNAL_COMPILE_FLAGS_ALT_SEP}
-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}
-DCMAKE_LINKER=${CMAKE_LINKER}
-DCMAKE_AR=${CMAKE_AR}
-DCMAKE_NM=${CMAKE_NM}
-DCMAKE_RANLIB=${CMAKE_RANLIB}
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
)
-DJERRY_GLOBAL_HEAP_SIZE=${JERRY_GLOBAL_HEAP_SIZE}
-DJERRY_CMDLINE=OFF
-DJERRY_ERROR_MESSAGES=ON
-DJERRY_LINE_INFO=ON
-DJERRY_EXTERNAL_CONTEXT=ON
-DJERRY_SNAPSHOT_EXEC=ON
-DJERRY_SNAPSHOT_SAVE=ON
-DJERRY_PARSER=ON
-DENABLE_LTO=OFF # FIXME: This option must be turned off or the cross-compiler settings will be overwritten
-DCMAKE_C_COMPILER_WORKS=true # cross-compiler
-DCMAKE_SYSTEM_NAME=Generic
-DCMAKE_SYSTEM_PROCESSOR=xtensa
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DEXTERNAL_COMPILE_FLAGS=${EXTERNAL_COMPILE_FLAGS_ALT_SEP}
-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}
-DCMAKE_LINKER=${CMAKE_LINKER}
-DCMAKE_AR=${CMAKE_AR}
-DCMAKE_NM=${CMAKE_NM}
-DCMAKE_RANLIB=${CMAKE_RANLIB}
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
)
add_dependencies(${COMPONENT_NAME} jerryscript_build)

set(JERRY_BUILD_PATH ${CMAKE_BINARY_DIR}/${COMPONENT_NAME}/jerryscript)

target_link_libraries(${COMPONENT_NAME}
${JERRY_BUILD_PATH}/lib/libjerry-core.a
${JERRY_BUILD_PATH}/lib/libjerry-ext.a
${JERRY_BUILD_PATH}/lib/libjerry-port-default-minimal.a)
${JERRY_BUILD_PATH}/lib/libjerry-core.a
${JERRY_BUILD_PATH}/lib/libjerry-ext.a
${JERRY_BUILD_PATH}/lib/libjerry-port-default-minimal.a)
4 changes: 2 additions & 2 deletions platforms/esp-idf/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "rt_node.h"
#include "rt-node.h"

int main(int argc ,char **argv) {
int app_main(int argc ,char **argv) {
rtnode_start();
return 0;
}
2 changes: 1 addition & 1 deletion platforms/unix/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "rt_node.h"
#include "rt-node.h"

int main(int argc ,char **argv) {
rtnode_start();
Expand Down
37 changes: 24 additions & 13 deletions platforms/unix/unix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,35 @@ include_directories(
deps/rtev/include
deps/rtev/src
include
src/internal
src
src/modules
)
set(SRCS
platforms/unix/main.c
src/js-binding.c
src/js-error.c
src/js-logger.c
src/js-main.c
src/js-modules.c
src/js-native.c
src/js-snapshots.c
src/js-common.c
src/js-rtev-watcher.c
src/modules/js-module-console.c
src/modules/js-module-process.c
src/modules/js-module-require.c
src/modules/js-module-timer.c
src/rtnode.c
src/rtnode-binding.c
src/rtnode-error.c
src/rtnode-logger.c
src/rtnode-modules.c
src/rtnode-native.c
src/rtnode-snapshots.c
src/rtnode-common.c
src/rtnode-rtev-watcher.c
src/modules/rtnode-module-console.c
src/modules/rtnode-module-process.c
src/modules/rtnode-module-require.c
src/modules/rtnode-module-timer.c
src/napi/node_api.c
# src/napi/node_api_async.c
src/napi/node_api_env.c
src/napi/node_api_function.c
src/napi/node_api_lifetime.c
src/napi/node_api_module.c
src/napi/node_api_object_wrap.c
src/napi/node_api_property.c
# src/napi/node_api_tsfn.c
src/napi/node_api_value.c
deps/rtev/src/allocator.c
deps/rtev/src/async.c
deps/rtev/src/context.c
Expand Down
Loading

0 comments on commit 5ce35e5

Please sign in to comment.