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.

legacy eventemitter action

+65 -5
+13
examples/spec/events.js
··· 126 126 eeD.emit('ctx'); 127 127 test('EventEmitter listeners receive emitter as this', listenerThis, eeD); 128 128 129 + function LegacyEmitter() { 130 + EventEmitter.call(this); 131 + } 132 + LegacyEmitter.prototype = {}; 133 + LegacyEmitter.prototype.__proto__ = EventEmitter.prototype; 134 + LegacyEmitter.prototype.constructor = LegacyEmitter; 135 + 136 + const legacy = new LegacyEmitter(); 137 + let legacyCount = 0; 138 + legacy.on('legacy', () => legacyCount++); 139 + legacy.emit('legacy'); 140 + test('EventEmitter.call(this) initializes classic inherited receivers', legacyCount, 1); 141 + 129 142 console.log('\nEventTarget Tests\n'); 130 143 131 144 const et = new EventTarget();
+15 -5
src/modules/events.c
··· 328 328 } 329 329 330 330 static ant_value_t js_eventemitter_ctor(ant_t *js, ant_value_t *args, int nargs) { 331 - if (vtype(js->new_target) == T_UNDEF) 332 - return js_mkerr_typed(js, JS_ERR_TYPE, "EventEmitter constructor requires 'new'"); 333 - 334 331 ant_value_t this_obj = js_getthis(js); 335 - if (is_object_type(this_obj)) js_set_slot(this_obj, SLOT_BRAND, js_mknum(BRAND_EVENTEMITTER)); 336 - return js_mkundef(); 332 + 333 + if (is_object_type(this_obj)) { 334 + js_set_slot(this_obj, SLOT_BRAND, js_mknum(BRAND_EVENTEMITTER)); 335 + return this_obj; 336 + } 337 + 338 + if (vtype(js->new_target) != T_UNDEF) { 339 + ant_value_t obj = js_mkobj(js); 340 + ant_value_t proto = js_instance_proto_from_new_target(js, g_eventemitter_proto); 341 + if (is_object_type(proto)) js_set_proto_init(obj, proto); 342 + js_set_slot(obj, SLOT_BRAND, js_mknum(BRAND_EVENTEMITTER)); 343 + return obj; 344 + } 345 + 346 + return js_mkerr_typed(js, JS_ERR_TYPE, "EventEmitter constructor requires an object receiver or 'new'"); 337 347 } 338 348 339 349 static ant_value_t js_event_preventDefault(ant_t *js, ant_value_t *args, int nargs) {
+37
tests/test_eventemitter_function_call.cjs
··· 1 + const { EventEmitter } = require('events'); 2 + 3 + function fail(message) { 4 + console.log(`FAIL: ${message}`); 5 + process.exit(1); 6 + } 7 + 8 + function Program() { 9 + EventEmitter.call(this); 10 + } 11 + 12 + Program.prototype = {}; 13 + Program.prototype.__proto__ = EventEmitter.prototype; 14 + Program.prototype.constructor = Program; 15 + 16 + const program = new Program(); 17 + 18 + if (!(program instanceof Program)) { 19 + fail('program should be an instance of Program'); 20 + } 21 + 22 + let seen = 0; 23 + program.on('tick', () => { 24 + seen++; 25 + }); 26 + program.emit('tick'); 27 + 28 + if (seen !== 1) { 29 + fail('EventEmitter.call(this) should initialize event methods on custom receivers'); 30 + } 31 + 32 + const ee = EventEmitter.call({}); 33 + if (typeof ee !== 'object') { 34 + fail('EventEmitter.call({}) should return the receiver object'); 35 + } 36 + 37 + console.log('PASS');