Skip to content

Commit 32bf840

Browse files
committed
debug
1 parent 41791ea commit 32bf840

File tree

1 file changed

+120
-24
lines changed

1 file changed

+120
-24
lines changed

javascript/core.js

Lines changed: 120 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ SOFTWARE.
2626

2727
(()=>{
2828
"use strict";
29+
alert('1: Script started');
2930

3031
const JUSTC = {};
3132
JUSTC.Checks = {
@@ -46,13 +47,37 @@ SOFTWARE.
4647
const isAMD = typeof define === 'function' && define.amd;
4748
const isModule = typeof module === 'object' && module.exports;
4849
const isBrowser = !isAMD && !isModule;
50+
alert(`2: Environment detected - isAMD: ${isAMD}, isModule: ${isModule}, isBrowser: ${isBrowser}`);
4951

5052
if (!isBrowser && typeof require === 'function') {
5153
try {
5254
JUSTC.NodeWASM = require('./justc.node.js')
5355
} catch (_) {}
5456
}
5557

58+
try {
59+
const globalThis_ = isBrowser ? []["filter"]["constructor"]("return globalThis")() || []["filter"]["constructor"]("return this")() || globalThis : globalThis || self || (isAMD ? {__justc__} : this) || {__justc__};
60+
alert('3: globalThis_ accessed successfully');
61+
if (!isBrowser) globalThis_.window = {};
62+
const OBJECT = Object;
63+
const json_ = JSON;
64+
const ARRAY = Array;
65+
const DOCUMENT = isBrowser ? document : null;
66+
const __URL__ = URL;
67+
const STRING = String;
68+
const ERR = Error;
69+
const CONSOLE = console;
70+
const MAP = Map;
71+
const BLOB = isBrowser ? Blob : null;
72+
const FETCH = fetch;
73+
74+
const isSafari = isBrowser ? /^((?!chrome|android).)*safari/i.test(globalThis_.navigator.userAgent) : false;
75+
alert(`4: Core objects initialized, isSafari: ${isSafari}`);
76+
} catch (error) {
77+
alert(`ERROR at core objects: ${error}`);
78+
throw error;
79+
}
80+
5681
const globalThis_ = isBrowser ? []["filter"]["constructor"]("return globalThis")() || []["filter"]["constructor"]("return this")() || globalThis : globalThis || self || (isAMD ? {__justc__} : this) || {__justc__};
5782
if (!isBrowser) globalThis_.window = {};
5883
const OBJECT = Object;
@@ -68,6 +93,7 @@ SOFTWARE.
6893
const FETCH = fetch;
6994

7095
const isSafari = isBrowser ? /^((?!chrome|android).)*safari/i.test(globalThis_.navigator.userAgent) : false;
96+
alert(`4: Core objects initialized, isSafari: ${isSafari}`);
7197

7298
JUSTC.VERSION = null;
7399
JUSTC.GetVersion = function() {
@@ -96,25 +122,39 @@ SOFTWARE.
96122
redefine: 'JUSTC cannot be redefined.',
97123
};
98124

125+
alert('5: JUSTC basic setup complete');
126+
99127
if (isBrowser) {
100-
JUSTC.Checks.sysFunc(OBJECT, ARRAY, __URL__, STRING, ERR, MAP, BLOB, FETCH);
101-
JUSTC.Checks.sysObj(json_, CONSOLE);
102-
JUSTC.Checks.sysObj(globalThis_, DOCUMENT);
103-
JUSTC.Checks.sysFunc(
104-
OBJECT.entries, OBJECT.defineProperty, OBJECT.freeze,
105-
json_.parse, json_.stringify,
106-
ARRAY.isArray, ARRAY.from,
107-
__URL__.parse,
108-
CONSOLE.log, CONSOLE.info, CONSOLE.error, CONSOLE.warn, CONSOLE.group, CONSOLE.groupEnd
109-
);
110-
JUSTC.Checks.sysFunc(DOCUMENT.createElement);
111-
JUSTC.Checks.sysObj(
112-
DOCUMENT.head,
113-
globalThis_.window
114-
);
115-
JUSTC.Checks.sysFunc(
116-
DOCUMENT.head.appendChild, DOCUMENT.head.removeChild
117-
);
128+
try {
129+
JUSTC.Checks.sysFunc(OBJECT, ARRAY, __URL__, STRING, ERR, MAP, BLOB, FETCH);
130+
alert('6.1: System functions check passed');
131+
JUSTC.Checks.sysObj(json_, CONSOLE);
132+
alert('6.2: System objects check passed');
133+
JUSTC.Checks.sysObj(globalThis_, DOCUMENT);
134+
alert('6.3: Global objects check passed');
135+
JUSTC.Checks.sysFunc(
136+
OBJECT.entries, OBJECT.defineProperty, OBJECT.freeze,
137+
json_.parse, json_.stringify,
138+
ARRAY.isArray, ARRAY.from,
139+
__URL__.parse,
140+
CONSOLE.log, CONSOLE.info, CONSOLE.error, CONSOLE.warn, CONSOLE.group, CONSOLE.groupEnd
141+
);
142+
alert('6.4: Object methods check passed');
143+
JUSTC.Checks.sysFunc(DOCUMENT.createElement);
144+
alert('6.5: Document methods check passed');
145+
JUSTC.Checks.sysObj(
146+
DOCUMENT.head,
147+
globalThis_.window
148+
);
149+
alert('6.6: DOM objects check passed');
150+
JUSTC.Checks.sysFunc(
151+
DOCUMENT.head.appendChild, DOCUMENT.head.removeChild
152+
);
153+
alert('6.7: DOM methods check passed');
154+
} catch (error) {
155+
alert(`ERROR in browser checks: ${error}`);
156+
throw error;
157+
}
118158
}
119159

120160
JUSTC.JUSTC = globalThis_.__justc__;
@@ -125,8 +165,16 @@ SOFTWARE.
125165
JUSTC.Silent = false;
126166
JUSTC.Experiments = false;
127167

128-
if (!isBrowser && !JUSTC.JUSTC && !JUSTC.WASM && JUSTC.NodeWASM) {JUSTC.JUSTC = JUSTC.NodeWASM}
129-
else if (isBrowser && !JUSTC.JUSTC && !JUSTC.WASM) throw new JUSTC.Error(JUSTC.Errors.environment);
168+
alert(`7: JUSTC.JUSTC = ${JUSTC.JUSTC}`);
169+
170+
if (!isBrowser && !JUSTC.JUSTC && !JUSTC.WASM && JUSTC.NodeWASM) {
171+
JUSTC.JUSTC = JUSTC.NodeWASM;
172+
alert('8: Using NodeWASM');
173+
}
174+
else if (isBrowser && !JUSTC.JUSTC && !JUSTC.WASM) {
175+
alert('ERROR: No WASM module found in browser');
176+
throw new JUSTC.Error(JUSTC.Errors.environment);
177+
}
130178

131179
JUSTC.Console = function(type, ...args) {
132180
if (!JUSTC.Silent) {
@@ -141,6 +189,7 @@ SOFTWARE.
141189

142190
JUSTC.Core = {};
143191
JUSTC.CoreScript = function(code, name) {
192+
alert(`9: CoreScript called for ${name}`);
144193
try {
145194
const resultptr = JUSTC.WASM.ccall(
146195
name,
@@ -157,11 +206,16 @@ SOFTWARE.
157206
);
158207
return json_.parse(resultjson);
159208
} catch (error) {
209+
alert(`ERROR in CoreScript: ${error}`);
160210
throw new JUSTC.Error(JUSTC.Errors[name + 'Error'], error);
161211
}
162212
};
163213
JUSTC.Core.Lexer = function Lexer(code) {
164-
if (!JUSTC.WASM) throw new JUSTC.Error(JUSTC.Errors.initWasm);
214+
alert('10: Lexer called');
215+
if (!JUSTC.WASM) {
216+
alert('ERROR: WASM not initialized in Lexer');
217+
throw new JUSTC.Error(JUSTC.Errors.initWasm);
218+
}
165219
if (!code || typeof code != 'string' || code.length < 1) throw new JUSTC.Error(JUSTC.Errors.wrongInputType);
166220
const result = JUSTC.CoreScript(code, 'lexer');
167221
if (result.error) {
@@ -171,7 +225,11 @@ SOFTWARE.
171225
}
172226
};
173227
JUSTC.Core.Parser = function Parser(code) {
174-
if (!JUSTC.WASM) throw new JUSTC.Error(JUSTC.Errors.initWasm);
228+
alert('11: Parser called');
229+
if (!JUSTC.WASM) {
230+
alert('ERROR: WASM not initialized in Parser');
231+
throw new JUSTC.Error(JUSTC.Errors.initWasm);
232+
}
175233
if (!code || typeof code != 'object') throw new JUSTC.Error(JUSTC.Errors.lexerInput);
176234
const result = JUSTC.CoreScript(JSON.stringify(code), 'parser');
177235
if (result.error) {
@@ -245,27 +303,36 @@ SOFTWARE.
245303
}
246304
};
247305

306+
alert('12: Private functions defined');
307+
248308
JUSTC.Initialize = async function() {
309+
alert('13: Initialize called');
249310
try {
250311
JUSTC.WASM = await JUSTC.JUSTC();
312+
alert('14: WASM initialized successfully');
251313
if (JUSTC.CoreLogsEnabled) {
252314
JUSTC.Console("log", "JUSTC WebAssembly module initialized.");
253315
}
254316
JUSTC.JUSTC = null;
255317
delete JUSTC.JUSTC;
256318
} catch (error) {
319+
alert(`ERROR in Initialize: ${error}`);
257320
JUSTC.Console("error", JUSTC.Errors.wasmFailed, error);
258321
}
259322
};
260323
JUSTC.InitWASM = async function InitializeJUSTC(attempt = 0) {
324+
alert(`15: InitWASM called, attempt ${attempt}`);
261325
while (!JUSTC.WASM) {
262326
attempt++;
327+
alert(`15.${attempt}: Initializing WASM, attempt ${attempt}`);
263328
await JUSTC.Initialize();
264329
if (attempt > 10) {
330+
alert('ERROR: WASM initialization failed after 10 attempts');
265331
throw new JUSTC.Error(JUSTC.Errors.wasmInitFailed);
266332
}
267333
};
268334
if (JUSTC.WASM) {
335+
alert('16: WASM available, updating private functions');
269336
for (const [unused, prfunc] of OBJECT.entries(JUSTC.PrivateFunctions.All)) {
270337
if (prfunc.NeedsWASM && !JUSTC.PrivateFunctions.Available.includes(prfunc.Name)) {
271338
JUSTC.PrivateFunctions.Available.push(prfunc.Name);
@@ -301,6 +368,7 @@ SOFTWARE.
301368
};
302369

303370
JUSTC.Parse = function(code, execute = false, outputMode = JUSTC.DefaultOutputMode) {
371+
alert('17: Parse called');
304372
try {
305373
const resultPtr = JUSTC.WASM.ccall(
306374
'parse',
@@ -316,11 +384,13 @@ SOFTWARE.
316384

317385
return result
318386
} catch (error) {
387+
alert(`ERROR in Parse: ${error}`);
319388
CONSOLE.error(JUSTC.Errors.executionError, error);
320389
throw error;
321390
}
322391
};
323392
JUSTC.AsyncParse = async function(code, execute, outputMode = JUSTC.DefaultOutputMode) {
393+
alert('18: AsyncParse called');
324394
return new Promise((resolve, reject) => {
325395
try {
326396
setTimeout(() => {
@@ -339,6 +409,7 @@ SOFTWARE.
339409

340410
resolve(result)
341411
} catch (error) {
412+
alert(`ERROR in AsyncParse setTimeout: ${error}`);
342413
console.error(JUSTC.Errors.executionError, error);
343414
reject(new JUSTC.Error(JUSTC.Errors.executionError, error))
344415
}
@@ -351,6 +422,7 @@ SOFTWARE.
351422

352423
JUSTC.CheckWASM = function() {
353424
if (!JUSTC.WASM) {
425+
alert('ERROR: CheckWASM failed - WASM not initialized');
354426
throw new JUSTC.Error(JUSTC.Errors.initWasm);
355427
}
356428
};
@@ -487,6 +559,7 @@ SOFTWARE.
487559
};
488560
JUSTC.Output = {
489561
parse: isBrowser || !JUSTC.Experiments ? function(code, outputMode = JUSTC.DefaultOutputMode) {
562+
alert('19: Output.parse called');
490563
JUSTC.Check(code);
491564
if (!JUSTC.OutputModes.includes(outputMode)) throw new JUSTC.Error(JUSTC.Errors.outputMode);
492565

@@ -501,6 +574,7 @@ SOFTWARE.
501574
return await JUSTC.AsyncOutput(false, code)
502575
},
503576
execute: isBrowser || !JUSTC.Experiments ? function(code, outputMode = JUSTC.DefaultOutputMode) {
577+
alert('20: Output.execute called');
504578
JUSTC.Check(code);
505579
if (!JUSTC.OutputModes.includes(outputMode)) throw new JUSTC.Error(JUSTC.Errors.outputMode);
506580

@@ -516,6 +590,7 @@ SOFTWARE.
516590
return await JUSTC.AsyncOutput(true, code)
517591
},
518592
initialize: async function() {
593+
alert('21: Output.initialize called');
519594
await JUSTC.InitWASM();
520595
},
521596
stringify: function(JavaScriptObjectNotation) {
@@ -534,6 +609,7 @@ SOFTWARE.
534609
return 'JUSTC'
535610
},
536611
get ["version"]() {
612+
alert('22: Public.version getter called');
537613
JUSTC.CheckWASM();
538614
const resultptr = JUSTC.WASM.ccall("version", "number");
539615
const result = JUSTC.WASM.UTF8ToString(resultptr);
@@ -623,14 +699,22 @@ SOFTWARE.
623699
return exports;
624700
};
625701

702+
alert('23: Starting final setup');
703+
626704
if (isBrowser) {
627-
if ("JUSTC" in globalThis_.window || "$JUSTC" in globalThis_.window) throw new JUSTC.Error(JUSTC.Errors.environment);
705+
alert('24: Browser environment setup');
706+
if ("JUSTC" in globalThis_.window || "$JUSTC" in globalThis_.window) {
707+
alert('ERROR: JUSTC or $JUSTC already in window');
708+
throw new JUSTC.Error(JUSTC.Errors.environment);
709+
}
628710
OBJECT.defineProperty(globalThis_.window, 'JUSTC', {
629711
get: function() {
712+
alert('25: window.JUSTC getter called');
630713
JUSTC.InitWASM();
631714
return OBJECT.freeze(JUSTC.Public);
632715
},
633716
set: function(command) {
717+
alert(`26: window.JUSTC setter called with: ${command}`);
634718
if (typeof command === 'string' && command.length > 0) {
635719
const vars = {
636720
"version": "Public.version",
@@ -761,14 +845,17 @@ SOFTWARE.
761845
});
762846
OBJECT.defineProperty(globalThis_.window, '$JUSTC', {
763847
get: function() {
848+
alert('27: window.$JUSTC getter called');
764849
return JUSTC.Output.execute;
765850
},
766851
set: function() {
852+
alert('ERROR: Trying to set $JUSTC');
767853
JUSTC.ErrorIfEnabled('$'+JUSTC.Errors.redefine);
768854
},
769855
configurable: false
770856
});
771857
if (!isSafari) setTimeout(async()=>{
858+
alert('28: Starting VFS registration');
772859
const RegisterSource = async function(url, vfs) {
773860
const text = await(await FETCH(url)).text();
774861
vfs.createFile(url, text, {
@@ -786,15 +873,24 @@ SOFTWARE.
786873
if (typeof source != 'string') continue;
787874
await RegisterSource(source, CurrentVFS);
788875
}
789-
} catch (_) {}
876+
alert('29: VFS registration completed');
877+
} catch (error) {
878+
alert(`ERROR in VFS registration: ${error}`);
879+
}
790880
},0);
881+
alert('30: Browser setup completed');
791882
} else if (isModule) {
883+
alert('31: Module environment');
792884
module.exports = JUSTC.CreateAsyncExports()
793885
} else if (isAMD) {
886+
alert('32: AMD environment');
794887
define(['require'], function(require) {
795888
return JUSTC.CreateAsyncExports()
796889
})
797890
} else {
891+
alert('ERROR: Unsupported environment');
798892
throw new JUSTC.Error('Unsupported environment.')
799893
}
894+
895+
alert('33: Script execution completed successfully');
800896
})()

0 commit comments

Comments
 (0)