Skip to content

Commit 52a0d2a

Browse files
committed
fix: restore source-map-support and adjust stack traces
1 parent a0acd77 commit 52a0d2a

10 files changed

Lines changed: 55 additions & 28 deletions

File tree

‎src/PHPContext.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,10 @@ export class PHPContext {
692692
*/
693693
public async callStaticMethod(className: string, method: string, args: any[] = [], targetObj?: any, originalClassName?: string): Promise<any> {
694694
const origClass = originalClassName || className;
695-
const lowerClass = className.toLowerCase();
696-
let cls = this.classes[className] || this.classes[lowerClass];
697-
if (!cls) cls = await this.engine.resolveClass(lowerClass, origClass, this);
695+
let cls = this.classes[className];
696+
if (!cls) cls = await this.engine.resolveClass(className, origClass, this);
698697
if (!cls) {
699-
const shortClassName = lowerClass.split("\\").pop() || lowerClass;
698+
const shortClassName = className.split("\\").pop() || className;
700699
cls = this.classes[shortClassName];
701700
}
702701
if (!cls) throw new PHPFatalError(`Class "${origClass}" not found`);

‎src/PHPEngine.js‎

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/PHPEngine.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/PHPEngine.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ export class PHPEngine {
391391

392392
const moduleObj = { exports: {} as any };
393393
try {
394-
const factory = new Function("module", "exports", "require", "PHPClass", "PHPObject", transpilation.code);
395-
factory(moduleObj, moduleObj.exports, require, PHPClass, PHPObject);
394+
const factory = new Function("module", "exports", "require", "PHPClass", "PHPObject", "PHPFatalError", transpilation.code);
395+
factory(moduleObj, moduleObj.exports, require, PHPClass, PHPObject, PHPFatalError);
396396
return moduleObj.exports;
397397
} catch (err: any) {
398398
if (err.name === "SyntaxError") {

‎src/parser/JSTranspiler.js‎

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/parser/JSTranspiler.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/parser/JSTranspiler.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ export class JSTranspiler {
460460
const safeId = name.replace(/[^a-zA-Z0-9_]/g, "_");
461461
const originalName = this.currentNamespaceName ? `${this.currentNamespaceName}\\${name}` : name;
462462
const qualifiedName = originalName.toLowerCase();
463-
// TODO: check if it's already defined and throw an exception
464-
lines.push(`${pad}var __cls_${safeId} = ctx.engine.classes[${JSON.stringify(qualifiedName)}] || new PHPClass(${JSON.stringify(originalName)});`);
463+
lines.push(`${pad}if (Object.hasOwn(ctx.classes, ${JSON.stringify(qualifiedName)})) throw new PHPFatalError(\`Cannot declare ${node.kind} ${originalName}, because the name is already in use\`);`);
464+
lines.push(`${pad}var __cls_${safeId} = ctx.classes[${JSON.stringify(qualifiedName)}] || new PHPClass(${JSON.stringify(originalName)});`);
465465
lines.push(`${pad}ctx.classes[${JSON.stringify(qualifiedName)}] = __cls_${safeId};`);
466466
break;
467467
}
@@ -481,7 +481,6 @@ export class JSTranspiler {
481481
const requiredCount = params.filter((p: any) => !p.hasDefault).length;
482482
const isGen = this.containsYield(node.body?.children || node.body);
483483

484-
// TODO: check if it's already defined and throw an exception
485484
lines.push(`${pad}async function${isGen ? "*" : ""} __fn_${safeFnId}(ctx, ...args) {`);
486485
lines.push(`${pad} ctx.pushScope();`);
487486
lines.push(`${pad} try {`);
@@ -502,6 +501,7 @@ export class JSTranspiler {
502501
lines.push(`${pad} }`);
503502
lines.push(`${pad}};`);
504503
lines.push(`${pad}__fn_${safeFnId}.phpMeta = { name: ${JSON.stringify(originalFuncName)}, visibility: ${JSON.stringify(visibility)}, numberOfParameters: ${params.length}, numberOfRequiredParameters: ${requiredCount}, parameters: ${JSON.stringify(params)} };`);
504+
lines.push(`${pad}if (Object.hasOwn(ctx.functions, ${JSON.stringify(funcName)})) throw new PHPFatalError(\`Cannot redeclare ${originalFuncName}()\`);`);
505505
lines.push(`${pad}ctx.functions[${JSON.stringify(funcName)}] = __fn_${safeFnId};`);
506506
break;
507507
}
@@ -516,8 +516,8 @@ export class JSTranspiler {
516516
this.currentClassName = qualifiedClassName;
517517
this.currentClassNameOriginal = originalClassName;
518518

519-
// TODO: check if it's already defined and throw an exception
520519
const parentClass = node.extends ? `(await ctx.resolveClass(${this.transpileClassReferenceLower(node.extends, filepath)}, ${this.transpileClassReferenceOriginal(node.extends, filepath)}))` : "undefined";
520+
lines.push(`${pad}if (Object.hasOwn(ctx.classes, ${JSON.stringify(qualifiedClassName)})) throw new PHPFatalError(\`Cannot declare class ${originalClassName}, because the name is already in use\`);`);
521521
lines.push(`${pad}var __cls_${safeClassId} = new PHPClass(${JSON.stringify(originalClassName)}, ${parentClass});`);
522522
lines.push(`${pad}ctx.classes[${JSON.stringify(qualifiedClassName)}] = __cls_${safeClassId};`);
523523

@@ -1159,7 +1159,7 @@ ${dummyLines.join("\n")}
11591159
}
11601160

11611161
case "goto": {
1162-
return throw new Error("GOTO is not implemented");
1162+
throw new Error("GOTO is not implemented");
11631163
}
11641164

11651165
case "assignref": {

‎src/runtime/PHPError.js‎

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)