js_vm_t* js_vm_new() { js_vm_t* vm = js_alloc(sizeof(js_vm_t)); // this proto/constructor is fixed up later by js_lib_initialize vm->global_scope = js_scope_make_global(vm, js_value_make_object(js_value_undefined(), js_value_undefined())); js_object_put(vm->global_scope->global_object, js_cstring("global"), vm->global_scope->global_object); js_lib_initialize(vm); return vm; }
static VAL js_object_base_get(js_value_t* obj, js_string_t* prop) { js_property_descriptor_t* descr = NULL; js_value_t* this = obj; while(!st_lookup(obj->object.properties, (st_data_t)prop, (st_data_t*)&descr)) { /* if not in object, look in prototype */ if(js_value_is_primitive(obj->object.prototype)) { /* do not attempt if prototype is primitive */ return js_value_undefined(); } obj = js_value_get_pointer(obj->object.prototype); } if(!descr->is_accessor) { return descr->data.value; } else { if(js_value_get_type(descr->accessor.get) == JS_T_FUNCTION) { return js_call(descr->accessor.get, js_value_make_pointer(this), 0, NULL); } return js_value_undefined(); } }
#include <stddef.h> #include <math.h> #include <limits.h> #include "lib.h" #ifndef M_PI #define M_PI (3.14159265358979323846) #endif static VAL Math_floor(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv) { double d = js_value_get_double(js_to_number(argc ? argv[0] : js_value_undefined())); return js_value_make_double(floor(d)); } static VAL Math_round(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv) { double d = js_value_get_double(js_to_number(argc ? argv[0] : js_value_undefined())); return js_value_make_double(floor(d + 0.5)); } static VAL Math_sin(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv) { double d = js_value_get_double(js_to_number(argc ? argv[0] : js_value_undefined())); return js_value_make_double(sin(d)); } static VAL Math_cos(js_vm_t* vm, void* state, VAL this, uint32_t argc, VAL* argv) { double d = js_value_get_double(js_to_number(argc ? argv[0] : js_value_undefined())); return js_value_make_double(cos(d));