1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-26 20:28:49 +08:00

add source map support

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.
This commit is contained in:
deoxxa
2016-04-28 23:06:29 +10:00
parent a55c9c9461
commit bd5fb254e3
11 changed files with 311 additions and 128 deletions

23
otto.go
View File

@@ -227,6 +227,7 @@ import (
"fmt"
"strings"
"github.com/robertkrimen/otto/file"
"github.com/robertkrimen/otto/registry"
)
@@ -289,7 +290,7 @@ func Run(src interface{}) (*Otto, Value, error) {
// src may also be a Program, but if the AST has been modified, then runtime behavior is undefined.
//
func (self Otto) Run(src interface{}) (Value, error) {
value, err := self.runtime.cmpl_run(src)
value, err := self.runtime.cmpl_run(src, nil)
if !value.safe() {
value = Value{}
}
@@ -307,7 +308,7 @@ func (self Otto) Eval(src interface{}) (Value, error) {
defer self.runtime.leaveScope()
}
value, err := self.runtime.cmpl_eval(src)
value, err := self.runtime.cmpl_eval(src, nil)
if !value.safe() {
value = Value{}
}
@@ -430,11 +431,17 @@ func (self Otto) Context() (ctx Context) {
ctx.Filename = "<unknown>"
ctx.Callee = frame.callee
if frame.file != nil {
ctx.Filename = frame.file.Name()
if ctx.Filename == "" {
ctx.Filename = "<anonymous>"
ctx.Filename = "<anonymous>"
p := frame.file.Position(file.Idx(frame.offset))
if p != nil {
ctx.Line = p.Line
ctx.Column = p.Column
if p.Filename != "" {
ctx.Filename = p.Filename
}
}
ctx.Line, ctx.Column = _position(frame.file, frame.offset)
}
// Get the current scope this Value
@@ -509,7 +516,7 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
}()
if !construct && this == nil {
program, err := self.runtime.cmpl_parse("", source+"()")
program, err := self.runtime.cmpl_parse("", source+"()", nil)
if err == nil {
if node, ok := program.body[0].(*_nodeExpressionStatement); ok {
if node, ok := node.expression.(*_nodeCallExpression); ok {
@@ -574,7 +581,7 @@ func (self Otto) Call(source string, this interface{}, argumentList ...interface
// If there is an error (like the source does not result in an object), then
// nil and an error is returned.
func (self Otto) Object(source string) (*Object, error) {
value, err := self.runtime.cmpl_run(source)
value, err := self.runtime.cmpl_run(source, nil)
if err != nil {
return nil, err
}