summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/compiler/bytecode_decode.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-04-06 22:18:25 +0200
committerGitHub <noreply@github.com>2019-04-06 22:18:25 +0200
commit115d20373c21b107a428a55247c64f900e116038 (patch)
treec7299b3d3be2a48c0f2d5bfbd856cbd1b27d2e55 /vendor/github.com/d5/tengo/compiler/bytecode_decode.go
parentcdf33e5748c110e12097130bdb44637e3d14b229 (diff)
downloadmatterbridge-msglm-115d20373c21b107a428a55247c64f900e116038.tar.gz
matterbridge-msglm-115d20373c21b107a428a55247c64f900e116038.tar.bz2
matterbridge-msglm-115d20373c21b107a428a55247c64f900e116038.zip
Update tengo vendor and load the stdlib. Fixes #789 (#792)
Diffstat (limited to 'vendor/github.com/d5/tengo/compiler/bytecode_decode.go')
-rw-r--r--vendor/github.com/d5/tengo/compiler/bytecode_decode.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/github.com/d5/tengo/compiler/bytecode_decode.go b/vendor/github.com/d5/tengo/compiler/bytecode_decode.go
new file mode 100644
index 00000000..b3b32a64
--- /dev/null
+++ b/vendor/github.com/d5/tengo/compiler/bytecode_decode.go
@@ -0,0 +1,97 @@
+package compiler
+
+import (
+ "encoding/gob"
+ "fmt"
+ "io"
+
+ "github.com/d5/tengo/objects"
+)
+
+// Decode reads Bytecode data from the reader.
+func (b *Bytecode) Decode(r io.Reader, modules *objects.ModuleMap) error {
+ if modules == nil {
+ modules = objects.NewModuleMap()
+ }
+
+ dec := gob.NewDecoder(r)
+
+ if err := dec.Decode(&b.FileSet); err != nil {
+ return err
+ }
+ // TODO: files in b.FileSet.File does not have their 'set' field properly set to b.FileSet
+ // as it's private field and not serialized by gob encoder/decoder.
+
+ if err := dec.Decode(&b.MainFunction); err != nil {
+ return err
+ }
+
+ if err := dec.Decode(&b.Constants); err != nil {
+ return err
+ }
+ for i, v := range b.Constants {
+ fv, err := fixDecoded(v, modules)
+ if err != nil {
+ return err
+ }
+ b.Constants[i] = fv
+ }
+
+ return nil
+}
+
+func fixDecoded(o objects.Object, modules *objects.ModuleMap) (objects.Object, error) {
+ switch o := o.(type) {
+ case *objects.Bool:
+ if o.IsFalsy() {
+ return objects.FalseValue, nil
+ }
+ return objects.TrueValue, nil
+ case *objects.Undefined:
+ return objects.UndefinedValue, nil
+ case *objects.Array:
+ for i, v := range o.Value {
+ fv, err := fixDecoded(v, modules)
+ if err != nil {
+ return nil, err
+ }
+ o.Value[i] = fv
+ }
+ case *objects.ImmutableArray:
+ for i, v := range o.Value {
+ fv, err := fixDecoded(v, modules)
+ if err != nil {
+ return nil, err
+ }
+ o.Value[i] = fv
+ }
+ case *objects.Map:
+ for k, v := range o.Value {
+ fv, err := fixDecoded(v, modules)
+ if err != nil {
+ return nil, err
+ }
+ o.Value[k] = fv
+ }
+ case *objects.ImmutableMap:
+ modName := moduleName(o)
+ if mod := modules.GetBuiltinModule(modName); mod != nil {
+ return mod.AsImmutableMap(modName), nil
+ }
+
+ for k, v := range o.Value {
+ // encoding of user function not supported
+ if _, isUserFunction := v.(*objects.UserFunction); isUserFunction {
+ return nil, fmt.Errorf("user function not decodable")
+ }
+
+ fv, err := fixDecoded(v, modules)
+ if err != nil {
+ return nil, err
+ }
+ o.Value[k] = fv
+ }
+ }
+
+ return o, nil
+}