format
This commit is contained in:
parent
ece62938a9
commit
a523a42e6e
10 changed files with 78 additions and 52 deletions
36
src/js.c
36
src/js.c
|
|
@ -5,20 +5,20 @@ duk_context* js_ctx;
|
|||
LOAD(js_core);
|
||||
LOAD(js_cmdline);
|
||||
|
||||
static duk_ret_t js_console_log(duk_context* self){
|
||||
static duk_ret_t js_console_log(duk_context* self) {
|
||||
printf("%s\n", duk_to_string(self, 0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void js_argv_init(int argc, char** argv){
|
||||
static void js_argv_init(int argc, char** argv) {
|
||||
int arr;
|
||||
int i;
|
||||
|
||||
duk_push_global_object(js_ctx);
|
||||
arr = duk_push_array(js_ctx);
|
||||
|
||||
for(i = 0; i < argc; i++){
|
||||
for(i = 0; i < argc; i++) {
|
||||
duk_push_string(js_ctx, argv[i]);
|
||||
duk_put_prop_index(js_ctx, arr, i);
|
||||
}
|
||||
|
|
@ -28,7 +28,7 @@ static void js_argv_init(int argc, char** argv){
|
|||
duk_pop(js_ctx);
|
||||
}
|
||||
|
||||
static void js_console_init(void){
|
||||
static void js_console_init(void) {
|
||||
int obj;
|
||||
|
||||
duk_push_global_object(js_ctx);
|
||||
|
|
@ -42,24 +42,24 @@ static void js_console_init(void){
|
|||
duk_pop(js_ctx);
|
||||
}
|
||||
|
||||
static duk_ret_t js_process_exit(duk_context* self){
|
||||
static duk_ret_t js_process_exit(duk_context* self) {
|
||||
duk_idx_t argc = duk_get_top(self);
|
||||
int ex;
|
||||
int ex;
|
||||
|
||||
if(argc > 1){
|
||||
if(argc > 1) {
|
||||
return duk_error(self, DUK_ERR_TYPE_ERROR, "needs 0 or 1 argument(s)");
|
||||
}
|
||||
|
||||
if(argc == 1){
|
||||
if(argc == 1) {
|
||||
ex = duk_to_number(self, 0);
|
||||
}else{
|
||||
} else {
|
||||
ex = 0;
|
||||
}
|
||||
|
||||
exit(ex);
|
||||
}
|
||||
|
||||
static void js_process_init(void){
|
||||
static void js_process_init(void) {
|
||||
int obj;
|
||||
|
||||
duk_push_global_object(js_ctx);
|
||||
|
|
@ -73,27 +73,27 @@ static void js_process_init(void){
|
|||
duk_pop(js_ctx);
|
||||
}
|
||||
|
||||
int js_init(int argc, char** argv){
|
||||
int js_init(int argc, char** argv) {
|
||||
js_ctx = duk_create_heap_default();
|
||||
|
||||
js_argv_init(argc, argv);
|
||||
js_console_init();
|
||||
js_process_init();
|
||||
|
||||
if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0){
|
||||
if(duk_peval_lstring(js_ctx, js_core, js_core_len) != 0) {
|
||||
printf("error: js/core.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define X(name) \
|
||||
if(duk_peval_lstring(js_ctx, compiler_ ## name, compiler_ ## name ## _len) != 0){ \
|
||||
printf("error: %s: %s\n", "compiler/" #name ".js", duk_safe_to_string(js_ctx, -1)); \
|
||||
#define X(name, prefix) \
|
||||
if(duk_peval_lstring(js_ctx, prefix##_##name, prefix##_##name##_len) != 0) { \
|
||||
printf("error: %s: %s\n", #prefix "/" #name ".js", duk_safe_to_string(js_ctx, -1)); \
|
||||
return 1; \
|
||||
}
|
||||
COMPILERS;
|
||||
JS;
|
||||
#undef X
|
||||
|
||||
if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0){
|
||||
if(duk_peval_lstring(js_ctx, js_cmdline, js_cmdline_len) != 0) {
|
||||
printf("error: js/cmdline.js: %s\n", duk_safe_to_string(js_ctx, -1));
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -101,6 +101,6 @@ int js_init(int argc, char** argv){
|
|||
return 0;
|
||||
}
|
||||
|
||||
void js_uninit(void){
|
||||
void js_uninit(void) {
|
||||
duk_destroy_heap(js_ctx);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue