加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang-otto JS解释器

发布时间:2020-12-16 18:39:26 所属栏目:大数据 来源:网络整理
导读:otto是一个Go语言实现的JavaScript 解释器 otto是一个Go语言实现的JavaScript的语法分析器和解释器 1 2 3 import ( "github.com/robertkrimen/otto" ) 在虚拟机中运行一些代码 3 4 5 vm := otto.New() vm.Run(` abc = 2 + ; console.log( "The value of abc

otto是一个Go语言实现的JavaScript 解释器

otto是一个Go语言实现的JavaScript的语法分析器和解释器

1
2
3
import (
"github.com/robertkrimen/otto"
)

在虚拟机中运行一些代码

3
4
5
vm := otto.New()
vm.Run(`
abc = 2 + ;
console.log( "The value of abc is " + abc); // 4
`)

从虚拟机中获取一些值

value,err := vm.Get( "abc" )
}

设置一个数字变量

vm.Set( "def" , 11 )
"The value of def is " + def);
// The value of def is 11
设置一个字符串变量

4
"xyzzy"
"Nothing happens." console.log(xyzzy.length); // 16
活动一个语句的运行结果

"xyzzy.length" {
// value is an int64 with a value of 16
一个错误发生时

5
6
"abcdefghijlmnopqrstuvwxyz.length"
)
if err != nil {
// err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
// If there is an error,then value.IsUndefined() is true
...
设置你个函数

"sayHello" fmt.Printf( "Hello,%s.n" 0 ). String ())
return otto.Value{}
})

设置你个带有返回值的函数

"twoPlus" right,_ := call.Argument( ).ToInteger()
result,_ := vm.ToValue( + right)
result
在JavaScript中使用函数

sayHello( "Xyzzy" ); // Hello,Xyzzy.
sayHello();
result = twoPlus( 2.0 // 4
`)

Parser

如果你只是对AST(抽象语法树)感兴趣,你可以获得一个分离的语法解析器

http://godoc.org/github.com/robertkrimen/otto/parser

解析并返回一个AST

6
7
8
9
10
11
12
13
14
15
16
17
filename :=
"" // A filename is optional
src := `
// Sample xyzzy example
( function (){
( 3.14159 > ) {
);
;
}
var xyzzy = NaN ;
);
xyzzy;
})();
`
// Parse some JavaScript,yielding a *ast.Program and/or an ErrorList
program,err := parser.ParseFile(nil,filename,src,monospace!important; font-size:12px!important; padding:0px!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important">)

otto

你也在命令行中运行otto

2
$ go
get -v github.com/robertkrimen/otto/otto
直接运行一段代码或运行一个源文件
1
$ otto example.js

使用

错误类型
type Error struct {
一个错误类型代一种运行时错误,类型错误或者引用错误

func (Error) Error
func (err Error) Error() string

错误返回错误描述

<pre>func (err Error) () string

错误返回错误描述和在那里发生的字符串

TypeError: 'def' is not a function
at xyz (<anonymous>: 3 : 9 )
at <anonymous>: 7 1 /

函数调用类型

1
2
3
4
5
type FunctionCall struct {
This Value
ArgumentList []Value
Otto *Otto
}

FunctionCall操作Javascript的函数调用

函数调用的参数

1
func (self FunctionCall) Argument(index int ) Value

Argument将会返回函数参数的索引,不存在将会返回Undefined

对象类型

2
type Object struct {
Object代表JavaScript 的对象类型

func (Object) Call

func (self ) Call(name string,argumentList ... interface {}) (Value,error)

调用对象的方法

本质上等同与

method,_ := object.Get(name)
method.Call(object,argumentList...)

func (Object) Class

) Class() string

Class 将会返回对象的类型,一下的一种

<code>Object
Function
Array
String
Number
Boolean
Date
RegExp
</code>

func (Object) Get

) Get(name string) (Value,sans-serif; font-size:15px; line-height:22.5px"> 获得给定名字的属性

func (Object) Keys

) Keys() []string

获得对象的键,等同于在对象上调用Object.keys

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读