大佬教程收集整理的这篇文章主要介绍了golang-otto JS解释器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
otto是一个Go语言实现的JavaScript 解释器
otto是一个Go语言实现的JavaScript的语法分析器和解释器
1
2
3
|
在虚拟机中运行一些代码
从虚拟机中获取一些值
设置一个数字变量
"The value of def is "
+ def);
// The value of def is 11
设置一个字符串变量
4
"xyzzy"
"Nothing happens."
console.log(xyzzy.length);
// 16
: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; font-family:Consolas,err = vm.Run(
"abcdefghijlmnopqrstuvwxyz.length"
)
// err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
// If there is an error,then value.IsUndefined() is true
...
设置你个带有返回值的函数
result,_ := vm.ToValue(
+ right)
result
如果你只是对AST(抽象语法树)感兴趣,你可以获得一个分离的语法解析器
http://godoc.org/github.com/robertkrimen/otto/parser
解析并返回一个AST
""
// A filename is optional
src := `
// Sample xyzzy example
:rgb(199,World."
);
;
}
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
-v github.com/robertkrimen/otto/otto
直接运行一段代码或运行一个源文件
错误返回错误描述
() String
错误返回错误描述和在那里发生的字符串
FunctionCall操作Javascript的函数调用
) Value
Argument将会返回函数参数的索引,不存在将会返回Undefined
调用对象的方法
本质上等同与
Class 将会返回对象的类型,一下的一种
<code>Object Function Array String number Boolean Date RegExp </code>
获得对象的键,等同于在对象上调用Object.keys
以上是大佬教程为你收集整理的golang-otto JS解释器全部内容,希望文章能够帮你解决golang-otto JS解释器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。