3500c686b3
- add type infos to VM error messages - add 'Name' to UserFunction objects - add 'expectErrorString' to VM tests - replace vm.expectError() with vm.expectErrorString() to make it more explicit - add source map info to VM error messages - optimization in function calls - add file/line/col info to compiler errors - change stdlib module to be loaded from VM (instead of compiler) so they can be properly loaded after the source is compiled into binary - VM can take builtin modules optionally
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package objects
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// to_json(v object) => bytes
|
|
func builtinToJSON(args ...Object) (Object, error) {
|
|
if len(args) != 1 {
|
|
return nil, ErrWrongNumArguments
|
|
}
|
|
|
|
res, err := json.Marshal(objectToInterface(args[0]))
|
|
if err != nil {
|
|
return &Error{Value: &String{Value: err.Error()}}, nil
|
|
}
|
|
|
|
return &Bytes{Value: res}, nil
|
|
}
|
|
|
|
// from_json(data string/bytes) => object
|
|
func builtinFromJSON(args ...Object) (Object, error) {
|
|
if len(args) != 1 {
|
|
return nil, ErrWrongNumArguments
|
|
}
|
|
|
|
var target interface{}
|
|
|
|
switch o := args[0].(type) {
|
|
case *Bytes:
|
|
err := json.Unmarshal(o.Value, &target)
|
|
if err != nil {
|
|
return &Error{Value: &String{Value: err.Error()}}, nil
|
|
}
|
|
case *String:
|
|
err := json.Unmarshal([]byte(o.Value), &target)
|
|
if err != nil {
|
|
return &Error{Value: &String{Value: err.Error()}}, nil
|
|
}
|
|
default:
|
|
return nil, ErrInvalidArgumentType{
|
|
Name: "first",
|
|
Expected: "bytes/string",
|
|
Found: args[0].TypeName(),
|
|
}
|
|
}
|
|
|
|
res, err := FromInterface(target)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|