Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: line offset while debugging on vscode/chrome with sourcemaps #211

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/worker-sdk6/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export async function startSceneRuntime(client: RpcClient) {
const sourceCode = await codeRequest.text()

// run the code of the scene
await customEval(sourceCode, runtimeExecutionContext)
await customEval(sourceCode, runtimeExecutionContext, isPreview.isPreview)
} catch (err) {
await EngineApi.sendBatch({ actions: [initMessagesFinished()] })

Expand Down
4 changes: 2 additions & 2 deletions src/worker-sdk6/runtime/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const allowListES5: Array<keyof typeof globalThis> = [
// eslint-disable-next-line @typescript-eslint/ban-types
const defer: (fn: Function) => void = (Promise.resolve().then as any).bind(Promise.resolve() as any)

export async function customEval(code: string, context: any) {
export async function customEval(code: string, context: any, previewMode: boolean) {
const sandbox: any = {}

const resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000)
Expand All @@ -53,7 +53,7 @@ export async function customEval(code: string, context: any) {
sandbox.window = sandbox
sandbox.self = sandbox

return defer(() => new Function('code', `with (this) { ${code} }`).call(sandbox, code))
return defer(() => new Function('code', previewMode ? `with (this) { ${code} }` : `with (this) { eval(${JSON.stringify(code)}) }`).call(sandbox, code))
}

function getES5Context(base: Record<string, any>) {
Expand Down
2 changes: 1 addition & 1 deletion src/worker-sdk7/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function startSceneRuntime(client: RpcClient) {
const sceneModule = createModuleRuntime(runtimeExecutionContext, clientPort, devToolsAdapter)

// run the code of the scene
await customEvalSdk7(sourceCode, runtimeExecutionContext)
await customEvalSdk7(sourceCode, runtimeExecutionContext, isPreview.isPreview)

if (!sceneModule.exports.onUpdate && !sceneModule.exports.onStart) {
// there may be cases where onStart is present and onUpdate not for "static-ish" scenes
Expand Down
4 changes: 2 additions & 2 deletions src/worker-sdk7/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const allowListES2020: Array<keyof typeof globalThis> = [
// eslint-disable-next-line @typescript-eslint/ban-types
const defer: (fn: Function) => void = (Promise.resolve().then as any).bind(Promise.resolve() as any)

export async function customEvalSdk7(code: string, context: Record<string | symbol, unknown>) {
const func = new Function('globalThis', `with (globalThis) {${code}}`)
export async function customEvalSdk7(code: string, context: Record<string | symbol, unknown>, previewMode: boolean) {
const func = new Function('globalThis', previewMode ? `with (globalThis) {eval(${JSON.stringify(code)})}`: `with (globalThis) {${code}}`)
const proxy: any = new Proxy(context, {
has() {
return true
Expand Down
40 changes: 20 additions & 20 deletions test/sandbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('Sandbox', () => {
throw new Error('1')
})

await expect(() => customEvalSdk7(example, { console: { log } })).rejects.toThrow('1')
await expect(() => customEvalSdk7(example, { console: { log } }, false)).rejects.toThrow('1')
expect(log).toBeCalled()
})

it('throw source map', async () => {
let stack = 'null'

try {
await customEvalSdk7(exampleFailingWithStack, {})
await customEvalSdk7(exampleFailingWithStack, {}, false)
} catch (err: any) {
stack = err.stack
}
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Sandbox', () => {
context.log = log
const sceneModule = createModuleRuntime(context, null as any, devToolsAdapter)

await customEvalSdk7(src, context)
await customEvalSdk7(src, context, false)

await sceneModule.exports.onUpdate!(0.0)
expect(log).toBeCalledWith(1) // evaluate new result
Expand All @@ -97,15 +97,15 @@ describe('Sandbox', () => {
it('this should be the proxy', async () => {
const log = jest.fn().mockImplementation((x) => { })

await customEvalSdk7(example, { console: { log } })
await customEvalSdk7(example, { console: { log } }, false)

expect(log).toBeCalledTimes(1)
})

it('this should be the proxy (eval)', async () => {
const log = jest.fn().mockImplementation((x) => { })

await customEvalSdk7(example, { console: { log } })
await customEvalSdk7(example, { console: { log } }, false)

expect(log).toBeCalledTimes(1)
})
Expand All @@ -116,7 +116,7 @@ describe('Sandbox', () => {
that = $
})

await customEvalSdk7(`console.log(globalThis)`, { console: { log } })
await customEvalSdk7(`console.log(globalThis)`, { console: { log } }, false)

expect(log).toBeCalledTimes(1)
expect(that.console).toHaveProperty('log')
Expand All @@ -139,9 +139,9 @@ describe('Sandbox', () => {
}
}
})
await customEvalSdk7('console.log()', context)
await customEvalSdk7('console.log()', context, false)
expect(i).toEqual(1)
await customEvalSdk7('console.log()', context)
await customEvalSdk7('console.log()', context, false)
expect(i).toEqual(2)
})

Expand All @@ -158,9 +158,9 @@ describe('Sandbox', () => {
}
}
})
await customEvalSdk7('console.log()', context)
await customEvalSdk7('console.log()', context, false)
expect(i).toEqual(1)
await customEvalSdk7('console.log()', context)
await customEvalSdk7('console.log()', context, false)
expect(i).toEqual(2)
})
})
Expand All @@ -187,7 +187,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(globalThis.${property})`, { x })
await customEvalSdk7(`x(globalThis.${property})`, { x }, false)

expect(x).toBeCalledTimes(1)

Expand All @@ -204,7 +204,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(globalThis.${property})`, { x })
await customEvalSdk7(`x(globalThis.${property})`, { x }, false)

expect(x).toBeCalledTimes(1)

Expand All @@ -221,7 +221,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(this.${property})`, { x })
await customEvalSdk7(`x(this.${property})`, { x }, false)

expect(x).toBeCalledTimes(1)

Expand All @@ -238,7 +238,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(this.${property})`, { x })
await customEvalSdk7(`x(this.${property})`, { x }, false)

expect(x).toBeCalledTimes(1)

Expand All @@ -255,7 +255,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(${property})`, { x })
await customEvalSdk7(`x(${property})`, { x }, false)

expect(x).toBeCalledTimes(1)
if (exists) {
Expand All @@ -271,7 +271,7 @@ export function checkExistence(property: string, exists: boolean) {
that = $
})

await customEvalSdk7(`x(${property})`, { x })
await customEvalSdk7(`x(${property})`, { x }, false)

expect(x).toBeCalledTimes(1)
if (exists) expect(that).not.toStrictEqual(undefined)
Expand All @@ -287,7 +287,7 @@ describe('dcl runtime', () => {
const sceneModule = createModuleRuntime(context, null as any, devToolsAdapter)

// run the code of the scene
await customEvalSdk7('exports.onUpdate = function() { return 123 }', context)
await customEvalSdk7('exports.onUpdate = function() { return 123 }', context, false)

expect(await sceneModule.exports.onUpdate!(0)).toEqual(123)
})
Expand All @@ -299,7 +299,7 @@ describe('dcl runtime', () => {
const sceneModule = createModuleRuntime(context, null as any, devToolsAdapter)

// run the code of the scene
await customEvalSdk7('exports.onUpdate = function() { return typeof setImmediate }', context)
await customEvalSdk7('exports.onUpdate = function() { return typeof setImmediate }', context, false)

expect(await sceneModule.exports.onUpdate!(0)).toEqual('function')
})
Expand All @@ -311,7 +311,7 @@ describe('dcl runtime', () => {
const context = Object.create({ fn })
const sceneModule = createModuleRuntime(context, null as any, devToolsAdapter)

await customEvalSdk7('setImmediate(fn)', context)
await customEvalSdk7('setImmediate(fn)', context, false)

expect(fn).not.toHaveBeenCalled()
await sceneModule.runStart!()
Expand All @@ -332,7 +332,7 @@ describe('dcl runtime', () => {
const context = Object.create(null)
const sceneModule = createModuleRuntime(context, null as any, devToolsAdapter)

await customEvalSdk7('exports.onStart = function() { if(setImmediate !== globalThis.setImmediate) throw new Error("they are different") }', context)
await customEvalSdk7('exports.onStart = function() { if(setImmediate !== globalThis.setImmediate) throw new Error("they are different") }', context, false)
await sceneModule.exports.onStart!()
})
})
Expand Down
Loading