Debugger-onNativeCall-06.js (2449B)
1 // Test that the onNativeCall hook is called when native function is 2 // called inside self-hosted JS. 3 4 load(libdir + 'eqArrayHelper.js'); 5 6 var g = newGlobal({ newCompartment: true }); 7 var dbg = new Debugger(); 8 var gdbg = dbg.addDebuggee(g); 9 10 let rv = []; 11 dbg.onNativeCall = (callee, reason) => { 12 rv.push(callee.name); 13 }; 14 15 gdbg.executeInGlobal(` 16 // Built-in native function. 17 [1, 2, 3].map(Array.prototype.push, Array.prototype); 18 19 // Built-in native function with non-optimized species lookup in 'map'. 20 var arr = [1, 2, 3]; 21 Object.setPrototypeOf(arr, Object.create(Array.prototype)); 22 arr.map(Array.prototype.push, Array.prototype); 23 24 // Self-hosted function. 25 [1, 2, 3].map(String.prototype.padStart, ""); 26 27 // Other native function. 28 [1, 2, 3].map(dateNow); 29 `); 30 assertEqArray(rv, [ 31 "map", "push", "push", "push", 32 "create", "setPrototypeOf", "map", "get [Symbol.species]", "push", "push", "push", 33 "map", "padStart", "padStart", "padStart", 34 "map", "dateNow", "dateNow", "dateNow", 35 ]); 36 rv = []; 37 gdbg.executeInGlobal(` 38 // Optimized 'match' (no callContentFunction). 39 var re = /a./; 40 "abc".match(re); 41 42 // Non-optimized 'match'. This calls RegExp.prototype[@@match] and getters on 43 // RegExp.prototype. 44 Object.setPrototypeOf(re, Object.create(RegExp.prototype)); 45 "abc".match(re); 46 `); 47 assertEqArray(rv, [ 48 "match", 49 "create", "setPrototypeOf", 50 "match", "[Symbol.match]", 51 "get flags", "get hasIndices", "get global", "get ignoreCase", "get multiline", 52 "get dotAll", "get unicode", "get unicodeSets", "get sticky", 53 ]); 54 rv = []; 55 gdbg.executeInGlobal(` 56 // Nested getters called internally inside self-hosted. 57 const r = /a./; 58 r.foo = 10; 59 "abc".match(r); 60 61 // Setter inside self-hosted JS. 62 // Hook "A.length = k" in Array.from. 63 var ctor = function() { 64 let obj = {}; 65 Object.defineProperty(obj, "length", { set: Array.prototype.join }); 66 return obj; 67 }; 68 var a = [1, 2, 3]; 69 a[Symbol.iterator] = null; 70 void Array.from.call(ctor, a); 71 `); 72 assertEqArray(rv, [ 73 "match", "[Symbol.match]", 74 "get flags", 75 "get hasIndices", "get global", "get ignoreCase", "get multiline", 76 "get dotAll", "get unicode", "get unicodeSets", "get sticky", 77 "call", "from", "defineProperty", "join", 78 ]); 79 80 rv = []; 81 gdbg.executeInGlobal(` 82 var origExec = RegExp.prototype.exec; 83 RegExp.prototype.exec = dateNow; 84 try { 85 (/a.b/).test("abc"); 86 } catch (e) {} // Throws not-object-or-null. 87 RegExp.prototype.exec = origExec; 88 `); 89 assertEqArray(rv, ["test", "dateNow"]);