Make html[...] and clear() compatible with QuickJS
dkl9

dkl9 commited on 2023-201 11:37:07
Showing 2 changed files, with 19 additions and 2 deletions.

... ...
@@ -189,13 +189,21 @@ export const BUILTIN_FUNCS = {
189 189
         return args["3"].evaluate(ide);
190 190
     }]], {}),
191 191
     "clear": new MathsFunc([[[], function(args, de) {
192
+        try {
192 193
             iol.innerHTML = "";
194
+        } catch (_e) {
195
+            wf("\x1b[2J");
196
+        }
193 197
         return 0;
194 198
     }]], {}),
195 199
     "tostring": new MathsFunc([[[IDENT_AST], function(args, de) {
200
+        try {
196 201
             const oe = document.createElement("p");
197 202
             oe.innerText = args["0"].toString();
198 203
             iol.append(oe);
204
+        } catch (_e) {
205
+            console.log(args["0"].toString());
206
+        }
199 207
         return 0;
200 208
     }]], {}),
201 209
     "render": new MathsFunc([[[IDENT_AST], function(args, de) {
... ...
@@ -205,10 +213,15 @@ export const BUILTIN_FUNCS = {
205 213
         return 0;
206 214
     }]], {}),
207 215
     "html": new MathsFunc([[[IDENT_AST], function(args, de) {
216
+        const ah = `<span class="maths"><table></table>${args["0"].toHTML()}</span>`;
217
+        try {
208 218
             const oe = document.createElement("pre");
209
-        oe.innerText = `<span class="maths"><table></table>${args["0"].toHTML()}</span>`;
219
+            oe.innerText = ah;
210 220
             iol.append(oe);
211 221
             return 0;
222
+        } catch (_e) {
223
+            console.log(ah);
224
+        }
212 225
     }]], {}),
213 226
     "lhs": new MathsFunc([[[IDENT_AST], function(args, de) {
214 227
         const ce = args["0"];
... ...
@@ -319,6 +332,8 @@ export const BUILTIN_FUNCS = {
319 332
     }]], {}),
320 333
 };
321 334
 
335
+AST.BUILTIN_FUNCS = BUILTIN_FUNCS;
336
+
322 337
 function dl(v) {
323 338
     // required by maths_parser, but doesn't actually have to do anything
324 339
     ioLog(v);
... ...
@@ -1,5 +1,7 @@
1 1
 "use strict";
2 2
 
3
+import {AST} from "maths_ast.js";
4
+
3 5
 // maths symbols for which the intuitive name matches the HTML entity (mostly Greek)
4 6
 const ENT_SYM = [
5 7
     "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta",
... ...
@@ -126,7 +128,7 @@ export const RTensorRender = {
126 128
                     `${cgify(sbu[0])}<sub>${sbu.slice(1).map(cgify).join("_")}</sub>`:
127 129
                     cgify(sbu[0]);
128 130
                 const spacify = sbu[0].replace(new RegExp(ucFromNcr("prime"), "g"), "").length >= 2 ? ` class="spaced"` : ``;
129
-                return (BUILTIN_FUNCS[this.v] != null || EXTRA_BUILTINS.has(this.v)) ?
131
+                return (AST.BUILTIN_FUNCS[this.v] != null || EXTRA_BUILTINS.has(this.v)) ?
130 132
                     `<var class="builtin">${it}</var>` :
131 133
                     `<var${spacify}>${it}</var>`;
132 134
                 break;
133 135