MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

eventemitter inherit from function/object

+126
+13
src/modules/events.c
··· 1232 1232 ant_value_t eventemitter_prototype(ant_t *js) { 1233 1233 if (g_eventemitter_proto) return g_eventemitter_proto; 1234 1234 1235 + ant_value_t object_proto = js->sym.object_proto; 1236 + ant_value_t function_proto = js_get_slot(js_glob(js), SLOT_FUNC_PROTO); 1237 + if (vtype(function_proto) == T_UNDEF) function_proto = js_get_ctor_proto(js, "Function", 8); 1238 + 1235 1239 ant_value_t eventemitter_ctor = js_mkobj(js); 1236 1240 ant_value_t eventemitter_proto = js_mkobj(js); 1241 + 1242 + if (is_object_type(object_proto)) js_set_proto_init(eventemitter_proto, object_proto); 1243 + if (is_object_type(function_proto)) js_set_proto_init(eventemitter_ctor, function_proto); 1237 1244 1238 1245 js_set(js, eventemitter_proto, "on", js_mkfun(js_eventemitter_on)); 1239 1246 js_set(js, eventemitter_proto, "addListener", js_mkfun(js_eventemitter_on)); ··· 1310 1317 ant_value_t pre_fn = js_make_ctor(js, js_promiserejectionevent_ctor, g_promiserejectionevent_proto, "PromiseRejectionEvent", 21); 1311 1318 js_set(js, global, "PromiseRejectionEvent", pre_fn); 1312 1319 1320 + ant_value_t object_proto = js->sym.object_proto; 1321 + ant_value_t function_proto = js_get_slot(global, SLOT_FUNC_PROTO); 1322 + if (vtype(function_proto) == T_UNDEF) function_proto = js_get_ctor_proto(js, "Function", 8); 1323 + 1313 1324 ant_value_t eventtarget_proto = js_mkobj(js); 1314 1325 g_eventtarget_proto = eventtarget_proto; 1326 + if (is_object_type(object_proto)) js_set_proto_init(eventtarget_proto, object_proto); 1315 1327 js_set(js, eventtarget_proto, "addEventListener", js_mkfun(js_add_event_listener_method)); 1316 1328 js_set(js, eventtarget_proto, "removeEventListener", js_mkfun(js_remove_event_listener_method)); 1317 1329 js_set(js, eventtarget_proto, "dispatchEvent", js_mkfun(js_dispatch_event_method)); 1318 1330 js_set_sym(js, eventtarget_proto, get_toStringTag_sym(), js_mkstr(js, "EventTarget", 11)); 1319 1331 1320 1332 ant_value_t eventtarget_ctor = js_mkobj(js); 1333 + if (is_object_type(function_proto)) js_set_proto_init(eventtarget_ctor, function_proto); 1321 1334 js_set_slot(eventtarget_ctor, SLOT_CFUNC, js_mkfun(js_eventtarget_ctor)); 1322 1335 js_mkprop_fast(js, eventtarget_ctor, "prototype", 9, eventtarget_proto); 1323 1336 js_mkprop_fast(js, eventtarget_ctor, "name", 4, ANT_STRING("EventTarget"));
+74
tests/bench_object_spread.cjs
··· 1 + const now = () => 2 + typeof performance !== 'undefined' && performance.now ? performance.now() : Date.now(); 3 + 4 + function bench(name, fn, iters = 1) { 5 + fn(); 6 + const t0 = now(); 7 + for (let i = 0; i < iters; i++) fn(); 8 + const dt = now() - t0; 9 + const per = (dt / iters).toFixed(6); 10 + console.log(`${name}: ${dt.toFixed(2)} ms total, ${per} ms/iter (${iters} iters)`); 11 + } 12 + 13 + const visibleSymbol = Symbol('visible'); 14 + const getterSymbol = Symbol('getter'); 15 + 16 + function makePlainSource() { 17 + return { 18 + a: 1, 19 + b: 2, 20 + c: 3, 21 + d: 4, 22 + e: 5, 23 + f: 6, 24 + }; 25 + } 26 + 27 + function makeMixedSource() { 28 + return { 29 + a: 1, 30 + b: 2, 31 + c: 3, 32 + d: 4, 33 + [visibleSymbol]: 5, 34 + }; 35 + } 36 + 37 + function makeGetterSource() { 38 + const obj = { 39 + a: 1, 40 + b: 2, 41 + c: 3, 42 + d: 4, 43 + }; 44 + 45 + Object.defineProperty(obj, getterSymbol, { 46 + enumerable: true, 47 + get() { 48 + return 42; 49 + }, 50 + }); 51 + 52 + return obj; 53 + } 54 + 55 + function spreadPlain() { 56 + const src = makePlainSource(); 57 + return { ...src }; 58 + } 59 + 60 + function spreadMixedSymbol() { 61 + const src = makeMixedSource(); 62 + return { ...src }; 63 + } 64 + 65 + function spreadSymbolGetter() { 66 + const src = makeGetterSource(); 67 + return { ...src }; 68 + } 69 + 70 + console.log('=== Object Spread Benchmark ===\n'); 71 + 72 + bench('spread plain object', spreadPlain, 200_000); 73 + bench('spread enumerable symbol', spreadMixedSymbol, 200_000); 74 + bench('spread enumerable symbol getter', spreadSymbolGetter, 100_000);
+39
tests/test_events_prototype_chain.cjs
··· 1 + const { EventEmitter } = require('events'); 2 + 3 + function fail(message) { 4 + console.log(`FAIL: ${message}`); 5 + process.exit(1); 6 + } 7 + 8 + if (Object.getPrototypeOf(EventEmitter.prototype) !== Object.prototype) { 9 + fail('EventEmitter.prototype should inherit from Object.prototype'); 10 + } 11 + 12 + function Program() {} 13 + 14 + if (typeof Program.prototype.__defineGetter__ !== 'function') { 15 + fail('fresh function prototypes should inherit legacy accessors'); 16 + } 17 + 18 + Program.prototype.__proto__ = EventEmitter.prototype; 19 + 20 + if (typeof Program.prototype.__defineGetter__ !== 'function') { 21 + fail('repointed prototypes should still inherit legacy accessors through EventEmitter.prototype'); 22 + } 23 + 24 + Program.prototype.__defineGetter__('terminal', function () { 25 + return this._terminal; 26 + }); 27 + 28 + const program = new Program(); 29 + program._terminal = 'xterm-256color'; 30 + 31 + if (program.terminal !== 'xterm-256color') { 32 + fail('getter installed after EventEmitter prototype reassignment should work'); 33 + } 34 + 35 + if (typeof EventTarget === 'function' && Object.getPrototypeOf(EventTarget.prototype) !== Object.prototype) { 36 + fail('EventTarget.prototype should inherit from Object.prototype'); 37 + } 38 + 39 + console.log('PASS');