add quickjs eval engine for macros

This commit is contained in:
Stewart Allen 2025-12-24 14:15:47 -05:00
commit 209f5cae9c
9 changed files with 404 additions and 10 deletions

View file

@ -2,6 +2,7 @@
import { arcToPath } from '../../geo/paths.js';
import { consts } from './consts.js';
import { createVM } from '../../moto/quickjs.js';
import { newPoint } from '../../geo/point.js';
import { util } from '../../geo/base.js';
@ -24,6 +25,7 @@ class Print {
this.tools = {};
// set to 1 to enable flow rate analysis (console)
this.debugE = settings ? (settings.controller.devel ? 1 : 0) : 0;
this._ready = this.createSafeEval();
}
setType(type) {
@ -237,7 +239,26 @@ class Print {
return output.last().point;
}
async ready() {
await this._ready;
}
async createSafeEval() {
this.safeEval = await createVM();
this.safeEval.eval("function range(a,b) { return (a + (layer / layers) * (b-a)) }");
}
disposeSafeEval() {
if (this.safeEval) {
this.safeEval.dispose();
}
}
constReplace(str, consts, start, pad, short) {
let safeEval = this.safeEval;
if (safeEval) {
safeEval.setContext(consts);
}
function tryeval(str) {
try {
return eval(`{ ${str} }`)
@ -269,7 +290,7 @@ class Print {
}
eva.push(`function range(a,b) { return (a + (layer / layers) * (b-a)).round(4) }`);
eva.push(`try {( ${tok} )} catch (e) {console.log(e);0}`);
let evl = tryeval(eva.join(''));
let evl = safeEval ? safeEval.eval(tok) : tryeval(eva.join(''));
nutok = evl;
if (pad === 666) {
return evl;

View file

@ -30,6 +30,9 @@ export async function cam_prepare(widgets, settings, update) {
const print = self.kiri_worker.current.print = newPrint(settings, active);
const { origin } = settings;
// wait for safe eval setup
await print.ready();
// cam-specific storage
print.output = [];

View file

@ -47,6 +47,9 @@ export async function fdm_prepare(widgets, settings, update) {
output = [],
layerout = [];
// wait for safe eval setup
await print.ready();
// compute bounds if missing
if (!bounds) {
bounds = new THREE.Box3();

View file

@ -58,6 +58,13 @@ function debug() {
console.log(...arguments);
}
function setPrint(print) {
if (current.print && current.print !== print) {
current.print.disposeSafeEval();
}
return current.print = print;
}
// catch clipper alerts and convert to console messages
self.alert = function(o) {
console.log(o);
@ -66,7 +73,7 @@ self.alert = function(o) {
self.uuid = ((Math.random() * Date.now()) | 0).toString(36);
/**
* @returns {RasterPath}
* @returns {RasterPath} instantiated class
*/
self.get_raster_gpu = async function({ mode, resolution, rotationStep }) {
let gpu = new RasterPath({
@ -314,7 +321,7 @@ const dispatch = {
// purge all sync data
clear(data, send) {
// current.snap = null;
current.print = null;
setPrint(null);
dispatch.group = wgroup = {};
dispatch.cache = worker.cache = wcache = {};
Widget.Groups.clear();
@ -427,7 +434,7 @@ const dispatch = {
let last = time(), now;
current.print = null;
setPrint(null);
current.mode = settings.mode.toUpperCase();
widget.anno = data.anno || widget.anno;
@ -496,7 +503,7 @@ const dispatch = {
send.data(emit, state.zeros);
}).then(() => {
const unitScale = settings.controller.units === 'in' ? (1 / 25.4) : 1;
const print = current.print || {};
const print = setPrint(current.print || {});
const minSpeed = (print.minSpeed || 0) * unitScale;
const maxSpeed = (print.maxSpeed || 0) * unitScale;
@ -571,7 +578,7 @@ const dispatch = {
z: origin.z - (process.camOriginOffZ ?? 0)
};
const device = settings.device;
const print = current.print = newPrint(settings, Object.values(wcache));
const print = setPrint(newPrint(settings, Object.values(wcache)));
const tools = device.extruders;
const mode = settings.mode;
const thin = settings.controller.lineType === 'line' || mode !== 'FDM';
@ -601,7 +608,7 @@ const dispatch = {
out.point = newPoint(x,y,z || 0);
});
});
const print = current.print = newPrint(null, Object.values(wcache));
const print = setPrint(newPrint(null, Object.values(wcache)));
render.path(parsed, progress => {
send.data({ progress });
}, { thin: true })