Go   发布时间:2019-10-06  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang-otto JS解释器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

在虚拟机中运行一些代码

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

从虚拟机中获取一些值

value,err := vm.Get( "abc" )
:rgb(199,_ := value.ToInteger()
}

设置一个数字变量

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
活动一个语句的运行结果

:1em 0px!important; position:relative!important; overflow-y:hidden!important; overflow-x:auto!important; font-size:12px!important; font-family:Consolas,_ = vm.Run( "xyzzy.length" {
// value is an int64 with a value of 16
:rgb(199,_ := value.ToInteger()
一个错误发生时

5
6
: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" )
@H_586_197@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" l.Argument( 0 ). @H_586_197@String ())
@H_586_197@return otto.Value{}
})

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

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

:1em 0px!important; position:relative!important; overflow-y:hidden!important; overflow-x:auto!important; font-size:12px!important; font-family:Consolas,_ = vm.Run(`
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
@H_616_349@ 17
filename :=
"" // A filename is optional
src := `
// Sample xyzzy example
( function (){
( 3.14159 > ) {
:rgb(199,World." );
;
}
var xyzzy = @H_586_197@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

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

使用

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

func (Error) Error
func (err Error) Error() String

错误返回错误描述

@H_944_543@
<pre>func (err Error) () String

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

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

函数调用类型

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

FunctionCall操作Javascript的函数调用

函数调用的参数

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

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

对象类型

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

func (Object) Call

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

调用对象的方法

本质上等同与

@H_55_723@method,_ := object.Get(Name)
@H_8_24@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

大佬总结

以上是大佬教程为你收集整理的golang-otto JS解释器全部内容,希望文章能够帮你解决golang-otto JS解释器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:golangjsotto解释器
猜你在找的Go相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap