int main(void) { enum v7_err rcode = V7_OK; struct v7 *v7 = v7_create(); v7_val_t ctor_func, proto, eval_result; proto = v7_mk_object(v7); ctor_func = v7_mk_function_with_proto(v7, MyThing_ctor, proto); v7_def(v7, ctor_func, "MY_CONST", ~0, (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(123)); v7_set_method(v7, proto, "myMethod", &MyThing_myMethod); v7_set(v7, v7_get_global(v7), "MyThing", ~0, ctor_func); rcode = v7_exec(v7, "\ print('MyThing.MY_CONST = ', MyThing.MY_CONST); \ var t = new MyThing(456); \ print('t.MY_CONST = ', t.MY_CONST); \ print('t.myMethod = ', t.myMethod); \ print('t.myMethod() = ', t.myMethod());", &eval_result); if (rcode != V7_OK) { fprintf(stderr, "exec error: %d\n", (int) rcode); } v7_destroy(v7); return (int) rcode; }
void init_updater_clubby(struct v7 *v7) { (void) v7; #ifndef DISABLE_C_CLUBBY #ifndef CS_DISABLE_JS s_v7 = v7; v7_val_t updater = v7_mk_object(v7); v7_val_t sys = v7_get(v7, v7_get_global(v7), "Sys", ~0); s_updater_notify_cb = V7_UNDEFINED; v7_own(v7, &s_updater_notify_cb); v7_def(v7, sys, "updater", ~0, V7_DESC_ENUMERABLE(0), updater); v7_set_method(v7, updater, "notify", Updater_notify); v7_set_method(v7, updater, "start", Updater_startupdate); v7_def(s_v7, updater, "GOT_REQUEST", ~0, (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(v7, UJS_GOT_REQUEST)); v7_def(s_v7, updater, "COMPLETED", ~0, (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(v7, UJS_COMPLETED)); v7_def(s_v7, updater, "NOTHING_TODO", ~0, (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(v7, UJS_NOTHING_TODO)); v7_def(s_v7, updater, "FAILED", ~0, (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(v7, UJS_ERROR)); #endif sj_clubby_register_global_command("/v1/SWUpdate.Update", handle_update_req, NULL); sj_clubby_register_global_command(clubby_cmd_ready, handle_clubby_ready, NULL); #endif }
V7_PRIVATE v7_val_t mk_cfunction_obj(struct v7 *v7, v7_cfunction_t *f, int num_args) { val_t obj = mk_object(v7, v7->vals.function_prototype); struct gc_tmp_frame tf = new_tmp_frame(v7); tmp_stack_push(&tf, &obj); v7_def(v7, obj, "", 0, _V7_DESC_HIDDEN(1), v7_mk_cfunction(f)); if (num_args >= 0) { v7_def(v7, obj, "length", 6, (V7_DESC_ENUMERABLE(0) | V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), v7_mk_number(v7, num_args)); } tmp_frame_cleanup(&tf); return obj; }
V7_PRIVATE v7_val_t mk_cfunction_obj_with_proto(struct v7 *v7, v7_cfunction_t *f, int num_args, v7_val_t proto) { struct gc_tmp_frame tf = new_tmp_frame(v7); v7_val_t res = mk_cfunction_obj(v7, f, num_args); tmp_stack_push(&tf, &res); v7_def(v7, res, "prototype", 9, (V7_DESC_ENUMERABLE(0) | V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0)), proto); v7_def(v7, proto, "constructor", 11, V7_DESC_ENUMERABLE(0), res); tmp_frame_cleanup(&tf); return res; }
V7_PRIVATE val_t mk_js_function(struct v7 *v7, struct v7_generic_object *scope, val_t proto) { struct v7_js_function *f; val_t fval = V7_NULL; struct gc_tmp_frame tf = new_tmp_frame(v7); tmp_stack_push(&tf, &proto); tmp_stack_push(&tf, &fval); f = new_function(v7); if (f == NULL) { /* fval is left `null` */ goto cleanup; } #if defined(V7_ENABLE_ENTITY_IDS) f->base.entity_id_base = V7_ENTITY_ID_PART_OBJ; f->base.entity_id_spec = V7_ENTITY_ID_PART_JS_FUNC; #endif fval = js_function_to_value(f); f->base.properties = NULL; f->scope = scope; /* * Before setting a `V7_OBJ_FUNCTION` flag, make sure we don't have * `V7_OBJ_DENSE_ARRAY` flag set */ assert(!(f->base.attributes & V7_OBJ_DENSE_ARRAY)); f->base.attributes |= V7_OBJ_FUNCTION; /* TODO(mkm): lazily create these properties on first access */ if (v7_is_object(proto)) { v7_def(v7, proto, "constructor", 11, V7_DESC_ENUMERABLE(0), fval); v7_def(v7, fval, "prototype", 9, V7_DESC_ENUMERABLE(0) | V7_DESC_CONFIGURABLE(0), proto); } cleanup: tmp_frame_cleanup(&tf); return fval; }
static void set_clubby(struct v7 *v7, v7_val_t obj, struct clubby *clubby) { v7_def(v7, obj, s_clubby_prop, sizeof(s_clubby_prop), (V7_DESC_WRITABLE(0) | V7_DESC_CONFIGURABLE(0) | _V7_DESC_HIDDEN(1)), v7_mk_foreign(v7, clubby)); }