mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
7 years ago
committed by
GitHub
29 changed files with 484 additions and 15 deletions
@ -0,0 +1,27 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
fulfill |
|||
object |
|||
[object Array] |
|||
0 |
|||
===*/ |
|||
|
|||
// Empty list is an important corner case, and also exercises the specific
|
|||
// .all() path where an immediate completion is checked for.
|
|||
|
|||
Promise.all([]).then(function (v) { |
|||
print('fulfill'); |
|||
print(typeof v); |
|||
print(Object.prototype.toString.call(v)); |
|||
print(v.length); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,29 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
O1.then |
|||
O2.then |
|||
Promise.all then |
|||
2 123 234 |
|||
===*/ |
|||
|
|||
var O1CB, O2CB; |
|||
var O1 = { then: function (cb) { print('O1.then'); O1CB = cb; } }; |
|||
var O2 = { then: function (cb) { print('O2.then'); O2CB = cb; } }; |
|||
var P = Promise.all([ O1, O2 ]); |
|||
P.then(function (v) { |
|||
print('Promise.all then'); |
|||
print(v.length, v[0], v[1]); |
|||
}); |
|||
|
|||
Promise.resolve().then(function () { |
|||
O1CB(123); |
|||
O2CB(234); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,32 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
fulfill, values match true |
|||
2 undefined undefined |
|||
===*/ |
|||
|
|||
// Missing argument and void 0 are handled the same.
|
|||
|
|||
var P = Promise.reject(); |
|||
var Q = Promise.reject(void 0); |
|||
|
|||
var T1 = P.catch(function (e) { |
|||
return e; |
|||
}); |
|||
var T2 = Q.catch(function (e) { |
|||
return e; |
|||
}); |
|||
|
|||
Promise.all([ T1, T2 ]).then(function (v) { |
|||
print('fulfill, values match', v[0] === v[1]); |
|||
print(v.length, v[0], v[1]); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,25 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
fulfill, values match true |
|||
2 undefined undefined |
|||
===*/ |
|||
|
|||
// Missing argument and void 0 are handled the same.
|
|||
|
|||
var P = Promise.resolve(); |
|||
var Q = Promise.resolve(void 0); |
|||
|
|||
Promise.all([ P, Q ]).then(function (v) { |
|||
print('fulfill, values match', v[0] === v[1]); |
|||
print(v.length, v[0], v[1]); |
|||
}, function (e) { |
|||
print('reject:', e); |
|||
}); |
|||
|
|||
print('done'); |
@ -0,0 +1,39 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
Q fulfill: 321 |
|||
===*/ |
|||
|
|||
// Promise executor return value has no effect on promise value.
|
|||
|
|||
var resolveP, rejectP; |
|||
var resolveQ, rejectQ; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolveP = resolve; |
|||
rejectP = reject; |
|||
return 123; // return value won't resolve P
|
|||
}); |
|||
P.then(function (v) { |
|||
print('P fulfill:', v); |
|||
}, function (e) { |
|||
print('P reject:', e); |
|||
}); |
|||
var Q = new Promise(function (resolve, reject) { |
|||
resolveQ = resolve; |
|||
rejectQ = reject; |
|||
return 123; |
|||
}); |
|||
Q.then(function (v) { |
|||
print('Q fulfill:', v); |
|||
}, function (e) { |
|||
print('Q reject:', e); |
|||
}); |
|||
|
|||
resolveQ(321); |
|||
|
|||
print('done'); |
@ -0,0 +1,34 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
ignored |
|||
done |
|||
fulfill: 123 |
|||
===*/ |
|||
|
|||
var resolveFn; |
|||
var P = new Promise(function (resolve, reject) { |
|||
resolveFn = resolve; |
|||
}); |
|||
|
|||
P.then(function (v) { |
|||
print('fulfill:', v); |
|||
}, function (e) { |
|||
print('reject:', e.name); |
|||
}); |
|||
|
|||
resolveFn(123); |
|||
|
|||
// Self resolution after already being settled is a no-op.
|
|||
try { |
|||
resolveFn(P); |
|||
print('ignored'); |
|||
} catch (e) { |
|||
print('should not happen:', e); |
|||
} |
|||
|
|||
print('done'); |
@ -0,0 +1,20 @@ |
|||
// Footprint of a Promise object and its resolve/reject function objects.
|
|||
|
|||
function test() { |
|||
var promises = []; |
|||
var resolves = []; |
|||
var rejects = []; |
|||
var executor = function (resolve, reject) { resolves.push(resolve); rejects.push(reject); }; |
|||
|
|||
while (promises.length < 1e5) { |
|||
if ((promises.length % 100) == 0) { |
|||
Duktape.gc(); |
|||
} |
|||
promises.push(new Promise(executor)); |
|||
} |
|||
Duktape.gc(); |
|||
|
|||
print(promises.length + ' Promises created'); |
|||
} |
|||
|
|||
test(); |
@ -0,0 +1,18 @@ |
|||
// Footprint of a pending Promise object, no resolve/reject functions.
|
|||
|
|||
function test() { |
|||
var promises = []; |
|||
function nop() {} |
|||
|
|||
while (promises.length < 1e5) { |
|||
if ((promises.length % 100) == 0) { |
|||
Duktape.gc(); |
|||
} |
|||
promises.push(new Promise(nop)); |
|||
} |
|||
Duktape.gc(); |
|||
|
|||
print(promises.length + ' Promises created'); |
|||
} |
|||
|
|||
test(); |
@ -0,0 +1,17 @@ |
|||
// Footprint of a rejected Promise object, no resolve/reject functions.
|
|||
|
|||
function test() { |
|||
var promises = []; |
|||
|
|||
while (promises.length < 1e5) { |
|||
if ((promises.length % 100) == 0) { |
|||
Duktape.gc(); |
|||
} |
|||
promises.push(Promise.reject(123)); |
|||
} |
|||
Duktape.gc(); |
|||
|
|||
print(promises.length + ' Promises created'); |
|||
} |
|||
|
|||
test(); |
@ -0,0 +1,17 @@ |
|||
// Footprint of a resolved Promise object, no resolve/reject functions.
|
|||
|
|||
function test() { |
|||
var promises = []; |
|||
|
|||
while (promises.length < 1e5) { |
|||
if ((promises.length % 100) == 0) { |
|||
Duktape.gc(); |
|||
} |
|||
promises.push(Promise.resolve(123)); |
|||
} |
|||
Duktape.gc(); |
|||
|
|||
print(promises.length + ' Promises created'); |
|||
} |
|||
|
|||
test(); |
@ -0,0 +1,22 @@ |
|||
// Footprint of an unresolved long .then() chain.
|
|||
|
|||
function test() { |
|||
var count = 1e3; |
|||
function id(v) { return v; } |
|||
|
|||
// Root and entire chain will remain unresolved.
|
|||
var root = new Promise(function () {}); |
|||
var curr = root; |
|||
|
|||
for (var i = 0; i < count; i++) { |
|||
if ((i % 100) == 0) { |
|||
Duktape.gc(); |
|||
} |
|||
curr = curr.then(id); |
|||
} |
|||
Duktape.gc(); |
|||
|
|||
print(count + ' Promises created'); |
|||
} |
|||
|
|||
test(); |
@ -0,0 +1,43 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
function mandel() { |
|||
var w = 200, h = 200, iter = 10; |
|||
var i, j, k, c; |
|||
var x0, y0, xx, yy, xx2, yy2; |
|||
var line; |
|||
|
|||
for (i = 0; i < h; i++) { |
|||
y0 = (i / h) * 2.5 - 1.25; |
|||
|
|||
for (j = 0, line = []; j < w; j++) { |
|||
x0 = (j / w) * 3.0 - 2.0; |
|||
|
|||
for (k = 0, xx = 0, yy = 0, c = '#'; k < iter; k++) { |
|||
xx2 = xx*xx; yy2 = yy*yy; |
|||
|
|||
if (xx2 + yy2 < 4.0) { |
|||
yy = 2*xx*yy + y0; |
|||
xx = xx2 - yy2 + x0; |
|||
} else { |
|||
/* xx^2 + yy^2 >= 4.0 */ |
|||
if (k < 3) { c = '.'; } |
|||
else if (k < 5) { c = ','; } |
|||
else if (k < 10) { c = '-'; } |
|||
else { c = '='; } |
|||
break; |
|||
} |
|||
} |
|||
|
|||
line.push(c); |
|||
} |
|||
|
|||
print(line.join('')); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
mandel(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
@ -0,0 +1,65 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
var jobs = []; |
|||
|
|||
function runJob(job) { |
|||
var iter = 10, k, c, xx, yy, xx2, yy2, x0 = job.x0, y0 = job.y0; |
|||
|
|||
for (k = 0, xx = 0, yy = 0, c = '#'; k < iter; k++) { |
|||
xx2 = xx*xx; yy2 = yy*yy; |
|||
|
|||
if (xx2 + yy2 < 4.0) { |
|||
yy = 2*xx*yy + y0; |
|||
xx = xx2 - yy2 + x0; |
|||
} else { |
|||
/* xx^2 + yy^2 >= 4.0 */ |
|||
if (k < 3) { c = '.'; } |
|||
else if (k < 5) { c = ','; } |
|||
else if (k < 10) { c = '-'; } |
|||
else { c = '='; } |
|||
job.resolve(c); |
|||
return; |
|||
} |
|||
} |
|||
job.resolve('#'); |
|||
} |
|||
|
|||
function createMandelPromise() { |
|||
var w = 200, h = 200; |
|||
var i, j; |
|||
var x0, y0; |
|||
var row; |
|||
var rows = []; |
|||
var P; |
|||
|
|||
for (i = 0; i < h; i++) { |
|||
y0 = (i / h) * 2.5 - 1.25; |
|||
|
|||
for (j = 0, row = []; j < w; j++) { |
|||
x0 = (j / w) * 3.0 - 2.0; |
|||
|
|||
P = new Promise(function (resolve, reject) { |
|||
jobs.push({ x0: x0, y0: y0, resolve: resolve, reject: reject }); |
|||
}); |
|||
row.push(P); |
|||
} |
|||
|
|||
rows.push(Promise.all(row)); |
|||
} |
|||
|
|||
return Promise.all(rows); |
|||
} |
|||
|
|||
try { |
|||
createMandelPromise().then(function (rows) { |
|||
rows.forEach(function (row) { |
|||
print(row.join('')); |
|||
}); |
|||
}, function (e) { |
|||
print(e); |
|||
}); |
|||
jobs.forEach(runJob); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
@ -0,0 +1,65 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
var jobs = []; |
|||
|
|||
function runJob(job) { |
|||
var iter = 100000, k, c, xx, yy, xx2, yy2, x0 = job.x0, y0 = job.y0; |
|||
|
|||
for (k = 0, xx = 0, yy = 0, c = '#'; k < iter; k++) { |
|||
xx2 = xx*xx; yy2 = yy*yy; |
|||
|
|||
if (xx2 + yy2 < 4.0) { |
|||
yy = 2*xx*yy + y0; |
|||
xx = xx2 - yy2 + x0; |
|||
} else { |
|||
/* xx^2 + yy^2 >= 4.0 */ |
|||
if (k < 3) { c = '.'; } |
|||
else if (k < 5) { c = ','; } |
|||
else if (k < 10) { c = '-'; } |
|||
else { c = '='; } |
|||
job.resolve(c); |
|||
return; |
|||
} |
|||
} |
|||
job.resolve('#'); |
|||
} |
|||
|
|||
function createMandelPromise() { |
|||
var w = 76, h = 28; |
|||
var i, j; |
|||
var x0, y0; |
|||
var row; |
|||
var rows = []; |
|||
var P; |
|||
|
|||
for (i = 0; i < h; i++) { |
|||
y0 = (i / h) * 2.5 - 1.25; |
|||
|
|||
for (j = 0, row = []; j < w; j++) { |
|||
x0 = (j / w) * 3.0 - 2.0; |
|||
|
|||
P = new Promise(function (resolve, reject) { |
|||
jobs.push({ x0: x0, y0: y0, resolve: resolve, reject: reject }); |
|||
}); |
|||
row.push(P); |
|||
} |
|||
|
|||
rows.push(Promise.all(row)); |
|||
} |
|||
|
|||
return Promise.all(rows); |
|||
} |
|||
|
|||
try { |
|||
createMandelPromise().then(function (rows) { |
|||
rows.forEach(function (row) { |
|||
print(row.join('')); |
|||
}); |
|||
}, function (e) { |
|||
print(e); |
|||
}); |
|||
jobs.forEach(runJob); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
Loading…
Reference in new issue