Skip to content

Commit d61e3fe

Browse files
committed
run svg-dos.js on Deno and Bun
1 parent 1b7704e commit d61e3fe

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

‎tests/svg-dos.js‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const panic = message => {
88
process.exit(1);
99
};
1010

11+
// (Bun's JavaScriptCore runs the rasterizer several times slower than V8: 12 s where Node takes 2 s)
12+
const slack = process.versions.bun ? 4 : 1;
1113
const timed = async (name, svg, limit = 5000) => {
14+
limit *= slack;
1215
const start = Date.now();
1316
let error = null, image = null;
1417
try { image = await Image.renderSVG(svg, {width: 100}); } catch (err) { error = err; }
@@ -139,9 +142,20 @@ const timed = async (name, svg, limit = 5000) => {
139142
for (let i = 3; i < across.bitmap.length; i += 4) b += across.bitmap[i];
140143
console.log(a, b);
141144
})();`;
142-
const child = require('child_process').spawnSync(process.execPath, ['--max-old-space-size=64', '-e', script], {encoding: 'utf8'});
143-
if (child.status !== 0) panic(`dashes in a 64 MB heap: ${child.stderr.split('\n').find(line => /heap|Error/.test(line)) ?? child.status}`);
144-
const [beside, across] = child.stdout.trim().split(' ').map(Number);
145+
// (the heap is limited with Node's flag: Deno and Bun take other ones, they run it in the test's process)
146+
let output;
147+
if (typeof Deno === 'undefined' && !process.versions.bun) {
148+
const child = require('child_process').spawnSync(process.execPath, ['--max-old-space-size=64', '-e', script], {encoding: 'utf8'});
149+
if (child.status !== 0) panic(`dashes in a 64 MB heap: ${child.stderr.split('\n').find(line => /heap|Error/.test(line)) ?? child.status}`);
150+
output = child.stdout;
151+
} else {
152+
const log = console.log;
153+
output = await new Promise(resolve => {
154+
console.log = (...values) => resolve(values.join(' '));
155+
new Function('require', script)(require);
156+
}).finally(() => { console.log = log; });
157+
}
158+
const [beside, across] = output.trim().split(' ').map(Number);
145159
// (half of the line shows: dashes of 0.1 with gaps as long; the zigzag about half of its length too)
146160
if (Math.abs(beside / (100 * 255) - 0.5) > 0.05) panic(`dashes beside the image: ${beside / 255} pixels covered of the line's 100, expected about half`);
147161
if (!(across > 0)) panic('the dashed zigzag shows nothing');
@@ -158,7 +172,7 @@ const timed = async (name, svg, limit = 5000) => {
158172
try { await Image.renderSVG(chain(600)); } catch (err) { error = err; }
159173
const time = Date.now() - start;
160174
if (!error || error.name !== 'LimitError') panic(`filter chain: expected a LimitError, got ${error} after ${time} ms`);
161-
if (time > 15000) panic(`filter chain: took ${time} ms`);
175+
if (time > 15000 * slack) panic(`filter chain: took ${time} ms`);
162176
error = null;
163177
try { await Image.renderSVG(chain(8), {maxLayerPixels: 4e6}); } catch (err) { error = err; }
164178
if (!error || error.name !== 'LimitError') panic(`filter chain within maxLayerPixels: expected a LimitError, got ${error}`);
@@ -168,7 +182,7 @@ const timed = async (name, svg, limit = 5000) => {
168182
const convolveStart = Date.now();
169183
error = null;
170184
try { await Image.renderSVG(convolve); } catch (err) { error = err; }
171-
if (!error || error.name !== 'LimitError' || Date.now() - convolveStart > 15000) panic(`a convolution of order 200: expected a LimitError within 15 s, got ${error} after ${Date.now() - convolveStart} ms`);
185+
if (!error || error.name !== 'LimitError' || Date.now() - convolveStart > 15000 * slack) panic(`a convolution of order 200: expected a LimitError within 15 s, got ${error} after ${Date.now() - convolveStart} ms`);
172186
// the work allowed scales with the size of the render: a drop shadow on a 4096x4096 image (rendered by rc.5) stays within it
173187
{
174188
const big = await Image.renderSVG(`<svg ${NS} width="4096" height="4096"><filter id="s" x="-10%" y="-10%" width="130%" height="130%"><feGaussianBlur in="SourceAlpha" stdDeviation="8"/><feOffset dx="10" dy="10" result="b"/><feFlood flood-color="#0004"/><feComposite in2="b" operator="in"/><feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge></filter><rect x="100" y="100" width="3800" height="3800" fill="red" filter="url(#s)"/></svg>`);
@@ -183,7 +197,7 @@ const timed = async (name, svg, limit = 5000) => {
183197
const begin = Date.now();
184198
error = null;
185199
try { await Image.renderSVG(`<svg ${NS} width="1000" height="1000"><filter id="f">${filter}</filter><rect width="1000" height="1000" filter="url(#f)"/></svg>`); } catch (err) { error = err; }
186-
if (!error || error.name !== 'LimitError' || Date.now() - begin > 15000) panic(`${name}: expected a LimitError within 15 s, got ${error} after ${Date.now() - begin} ms`);
200+
if (!error || error.name !== 'LimitError' || Date.now() - begin > 15000 * slack) panic(`${name}: expected a LimitError within 15 s, got ${error} after ${Date.now() - begin} ms`);
187201
}
188202
// a filter region is a layer like the others: over maxLayerPixels it's a LimitError, not a silently missing
189203
// element, and the limit is what the option says (a fixed 2^26 pixels refused a filter on the whole of 6900x6900)
@@ -201,7 +215,7 @@ const timed = async (name, svg, limit = 5000) => {
201215
const begin = Date.now();
202216
error = null;
203217
try { await Image.renderSVG(markers); } catch (err) { error = err; }
204-
if (!error || !/markers/.test(error.message) || Date.now() - begin > 30000) panic(`120000 markers: expected the marker limit within 30 s, got ${error} after ${Date.now() - begin} ms`);
218+
if (!error || !/markers/.test(error.message) || Date.now() - begin > 30000 * slack) panic(`120000 markers: expected the marker limit within 30 s, got ${error} after ${Date.now() - begin} ms`);
205219
}
206220
// a real filter on a large image still renders
207221
const shadow = await Image.renderSVG(`<svg ${NS} width="2000" height="2000"><filter id="s"><feGaussianBlur in="SourceAlpha" stdDeviation="8"/><feOffset dx="10" dy="10" result="b"/><feFlood flood-color="#0004"/><feComposite in2="b" operator="in"/><feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge></filter><rect x="100" y="100" width="1700" height="1700" fill="red" filter="url(#s)"/></svg>`);

0 commit comments

Comments
 (0)