mirror of
https://github.com/robertkrimen/otto
synced 2025-10-12 20:27:30 +08:00

This patch implements source map support in the parser, the runtime, the script record, and the stack trace printing. The library used to parse and use the source maps is gopkg.in/sourcemap.v1. Unlike earlier versions of this patch, the consumer of otto does not need parse the source map on their own - it's now handled similarly to parsing JavaScript content. To use a source map, the consumer must explicitly parse their source into a `Script` object with `Otto.CompileWithSourceMap`. The script record returned from that call will carry source map information with it, and all location-related functions should reflect the original source positions.
141 lines
3.4 KiB
Go
141 lines
3.4 KiB
Go
package otto
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
testSourcemapCodeOriginal = "function functionA(argA, argB) {\n functionB(argA, argB);\n}\n\nfunction functionB(argA, argB) {\n functionExternal(argA, argB);\n}"
|
|
testSourcemapCodeMangled = "function functionA(argA,argB){functionB(argA,argB)}function functionB(argA,argB){functionExternal(argA,argB)}"
|
|
testSourcemapContent = `{"version":3,"sources":["hello.js"],"names":["functionA","argA","argB","functionB","functionExternal"],"mappings":"AAAA,QAASA,WAAUC,KAAMC,MACvBC,UAAUF,KAAMC,MAGlB,QAASC,WAAUF,KAAMC,MACvBE,iBAAiBH,KAAMC"}`
|
|
testSourcemapOriginalStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:3)\n at functionA (hello.js:2:3)\n at <anonymous>:1:1\n"
|
|
testSourcemapMangledStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:1:82)\n at functionA (hello.js:1:31)\n at <anonymous>:1:1\n"
|
|
testSourcemapMappedStack = "ReferenceError: 'functionExternal' is not defined\n at functionB (hello.js:6:2)\n at functionA (hello.js:2:2)\n at <anonymous>:1:1\n"
|
|
)
|
|
|
|
func TestSourceMapOriginalWithNoSourcemap(t *testing.T) {
|
|
tt(t, func() {
|
|
vm := New()
|
|
|
|
s, err := vm.Compile("hello.js", testSourcemapCodeOriginal)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := vm.Run(s); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
_, err = vm.Run(`functionA()`)
|
|
if err == nil {
|
|
panic("error should not be nil")
|
|
}
|
|
|
|
is(err.(*Error).String(), testSourcemapOriginalStack)
|
|
})
|
|
}
|
|
|
|
func TestSourceMapMangledWithNoSourcemap(t *testing.T) {
|
|
tt(t, func() {
|
|
vm := New()
|
|
|
|
s, err := vm.Compile("hello.js", testSourcemapCodeMangled)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := vm.Run(s); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
_, err = vm.Run(`functionA()`)
|
|
if err == nil {
|
|
panic("error should not be nil")
|
|
}
|
|
|
|
is(err.(*Error).String(), testSourcemapMangledStack)
|
|
})
|
|
}
|
|
|
|
func TestSourceMapMangledWithSourcemap(t *testing.T) {
|
|
tt(t, func() {
|
|
vm := New()
|
|
|
|
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := vm.Run(s); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
_, err = vm.Run(`functionA()`)
|
|
if err == nil {
|
|
panic("error should not be nil")
|
|
}
|
|
|
|
is(err.(*Error).String(), testSourcemapMappedStack)
|
|
})
|
|
}
|
|
|
|
func TestSourceMapContextPosition(t *testing.T) {
|
|
tt(t, func() {
|
|
vm := New()
|
|
|
|
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := vm.Run(s); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
vm.Set("functionExternal", func(c FunctionCall) Value {
|
|
ctx := c.Otto.Context()
|
|
|
|
is(ctx.Filename, "hello.js")
|
|
is(ctx.Line, 6)
|
|
is(ctx.Column, 2)
|
|
|
|
return UndefinedValue()
|
|
})
|
|
|
|
if _, err := vm.Run(`functionA()`); err != nil {
|
|
panic(err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSourceMapContextStacktrace(t *testing.T) {
|
|
tt(t, func() {
|
|
vm := New()
|
|
|
|
s, err := vm.CompileWithSourceMap("hello.js", testSourcemapCodeMangled, testSourcemapContent)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := vm.Run(s); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
vm.Set("functionExternal", func(c FunctionCall) Value {
|
|
ctx := c.Otto.Context()
|
|
|
|
is(ctx.Stacktrace, []string{
|
|
"functionB (hello.js:6:2)",
|
|
"functionA (hello.js:2:2)",
|
|
"<anonymous>:1:1",
|
|
})
|
|
|
|
return UndefinedValue()
|
|
})
|
|
|
|
if _, err := vm.Run(`functionA()`); err != nil {
|
|
panic(err)
|
|
}
|
|
})
|
|
}
|