Skip to content

Commit

Permalink
[aio] Use prototype inheritance to implement AIO functions (zephyrpro…
Browse files Browse the repository at this point in the history
…ject-rtos#498)

Also, add a spot for a cleanup function to be registered with modules
that will be called by a new zjs_modules_cleanup function. Currently,
this is called only in ashell.

Also, fix a bug where module instances weren't being released.

Signed-off-by: Geoff Gustafson <geoff@linux.intel.com>
  • Loading branch information
grgustaf authored and Jimmy Huang committed Dec 5, 2016
1 parent a162db1 commit d4e1d64
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/ashell/jerry-code.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ void javascript_eval_code(const char *source_buffer)
jerry_release_value(ret_val);
}


void restore_zjs_api() {
#ifdef ZJS_POOL_CONFIG
zjs_init_mem_pools();
Expand Down Expand Up @@ -161,6 +160,7 @@ void javascript_stop()
zjs_timers_cleanup();
zjs_ipm_free_callbacks();
zjs_buffer_cleanup();
zjs_modules_cleanup();
jerry_cleanup();

restore_zjs_api();
Expand Down
21 changes: 17 additions & 4 deletions src/zjs_aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define ZJS_AIO_TIMEOUT_TICKS 500

static struct k_sem aio_sem;
static jerry_value_t zjs_aio_prototype;

#define MAX_TYPE_LEN 20

Expand Down Expand Up @@ -324,10 +325,7 @@ static jerry_value_t zjs_aio_open(const jerry_value_t function_obj,

// create the AIOPin object
jerry_value_t pinobj = jerry_create_object();
zjs_obj_add_function(pinobj, zjs_aio_pin_read, "read");
zjs_obj_add_function(pinobj, zjs_aio_pin_read_async, "readAsync");
zjs_obj_add_function(pinobj, zjs_aio_pin_close, "close");
zjs_obj_add_function(pinobj, zjs_aio_pin_on, "on");
jerry_set_prototype(pinobj, zjs_aio_prototype);
zjs_obj_add_string(pinobj, name, "name");
zjs_obj_add_number(pinobj, device, "device");
zjs_obj_add_number(pinobj, pin, "pin");
Expand All @@ -343,11 +341,26 @@ jerry_value_t zjs_aio_init()

k_sem_init(&aio_sem, 0, 1);

zjs_native_func_t array[] = {
{ zjs_aio_pin_read, "read" },
{ zjs_aio_pin_read_async, "readAsync" },
{ zjs_aio_pin_close, "close" },
{ zjs_aio_pin_on, "on" },
{ NULL, NULL }
};
zjs_aio_prototype = jerry_create_object();
zjs_obj_add_functions(zjs_aio_prototype, array);

// create global AIO object
jerry_value_t aio_obj = jerry_create_object();
zjs_obj_add_function(aio_obj, zjs_aio_open, "open");
return aio_obj;
}

void zjs_aio_cleanup()
{
jerry_release_value(zjs_aio_prototype);
}

#endif // QEMU_BUILD
#endif // BUILD_MODULE_AIO
4 changes: 4 additions & 0 deletions src/zjs_aio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

#include "jerry-api.h"

/** Initialize the aio module, or reinitialize after cleanup */
jerry_value_t zjs_aio_init();

/** Release resources held by the aio module */
void zjs_aio_cleanup();

#endif // __zjs_aio_h__
32 changes: 23 additions & 9 deletions src/zjs_modules.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@
#endif
#endif

typedef jerry_value_t (*initcb_t)();
typedef void (*cleanupcb_t)();

typedef struct module {
const char *name;
initcb_t init;
cleanupcb_t cleanup;
jerry_value_t instance;
} module_t;

// init function is required, cleanup is optional in these entries
module_t zjs_modules_array[] = {
#ifndef ZJS_LINUX_BUILD
#ifndef QEMU_BUILD
#ifndef CONFIG_BOARD_FRDM_K64F
#ifdef BUILD_MODULE_AIO
{ "aio", zjs_aio_init },
{ "aio", zjs_aio_init, zjs_aio_cleanup },
#endif
#endif
#ifdef BUILD_MODULE_BLE
Expand Down Expand Up @@ -116,7 +121,7 @@ static jerry_value_t native_require_handler(const jerry_value_t function_obj,
for (int i = 0; i < modcount; i++) {
module_t *mod = &zjs_modules_array[i];
if (!strcmp(mod->name, module)) {
// We only want one intance of each module at a time
// We only want one instance of each module at a time
if (mod->instance == 0) {
mod->instance = jerry_acquire_value(mod->init());
}
Expand Down Expand Up @@ -162,20 +167,29 @@ static jerry_value_t native_require_handler(const jerry_value_t function_obj,

void zjs_modules_init()
{
int modcount = sizeof(zjs_modules_array) / sizeof(module_t);

for (int i = 0; i < modcount; i++) {
module_t *mod = &zjs_modules_array[i];
mod->instance = 0;
}

jerry_value_t global_obj = jerry_get_global_object();

// create the C handler for require JS call
zjs_obj_add_function(global_obj, native_require_handler, "require");
jerry_release_value(global_obj);
}

void zjs_modules_cleanup()
{
int modcount = sizeof(zjs_modules_array) / sizeof(module_t);

for (int i = 0; i < modcount; i++) {
module_t *mod = &zjs_modules_array[i];
if (mod->instance) {
if (mod->cleanup) {
mod->cleanup();
}
jerry_release_value(mod->instance);
mod->instance = 0;
}
}
}

void zjs_register_service_routine(void* handle, zjs_service_routine func)
{
if (num_routines >= NUM_SERVICE_ROUTINES) {
Expand Down
2 changes: 1 addition & 1 deletion src/zjs_modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

#define NUM_SERVICE_ROUTINES 3

typedef jerry_value_t (*initcb_t)();
typedef void (*zjs_service_routine)(void* handle);

void zjs_modules_init();
void zjs_modules_cleanup();
void zjs_register_service_routine(void* handle, zjs_service_routine func);
void zjs_service_routines(void);

Expand Down

0 comments on commit d4e1d64

Please sign in to comment.