golang-otto JS解释器

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

 

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

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

在虚拟机中运行一些代码

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

从虚拟机中获取一些值

1
2
3
value, err := vm.Get( "abc" )
     value, _ := value.ToInteger()
}

设置一个数字变量

1
2
3
4
5
vm.Set( "def" 11 )
vm.Run(`
     console.log( "The value of def is " + def);
     // The value of def is 11
`)

设置一个字符串变量

1
2
3
4
vm.Set( "xyzzy" "Nothing happens." )
vm.Run(`
     console.log(xyzzy.length); // 16
`)

活动一个语句的运行结果

1
2
3
4
5
value, _ = vm.Run( "xyzzy.length" )
{
     // value is an int64 with a value of 16
     value, _ := value.ToInteger()
}

一个错误发生时

1
2
3
4
5
6
value, err = vm.Run( "abcdefghijlmnopqrstuvwxyz.length" )
if err != nil {
     // err = ReferenceError: abcdefghijlmnopqrstuvwxyz is not defined
     // If there is an error, then value.IsUndefined() is true
     ...
}

设置你个函数

1
2
3
4
vm.Set( "sayHello" , func(call otto.FunctionCall) otto.Value {
     fmt.Printf( "Hello, %s.\n" , call.Argument( 0 ). String ())
     return otto.Value{}
})

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

1
2
3
4
5
vm.Set( "twoPlus" , func(call otto.FunctionCall) otto.Value {
     right, _ := call.Argument( 0 ).ToInteger()
     result, _ := vm.ToValue( 2 + right)
     return result
})

在JavaScript中使用函数

1
2
3
4
5
6
result, _ = vm.Run(`
     sayHello( "Xyzzy" );      // Hello, Xyzzy.
     sayHello();             // Hello, undefined
 
     result = twoPlus( 2.0 ); // 4
`)

Parser

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

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

解析并返回一个AST

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
filename :=  "" // A filename is optional
src := `
     // Sample xyzzy example
     ( function (){
         if ( 3.14159 0 ) {
             console.log( "Hello, World." );
             return ;
         }
 
         var xyzzy =  NaN ;
         console.log( "Nothing happens." );
         return xyzzy;
     })();
`
 
// Parse some JavaScript, yielding a *ast.Program and/or an ErrorList
program, err := parser.ParseFile(nil, filename, src,  0 )

otto

你也在命令行中运行otto

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

使用

1
 

 

1
错误类型
1
2
type Error struct {
}

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

1
2
3
func (Error) Error
 
func (err Error) Error() string

错误返回错误描述

1
 

 

1
<pre>func (err Error)  String () string

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

1
2
3
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

对象类型
1
2
type  Object struct {
}

Object代表JavaScript 的对象类型

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

调用对象的方法

本质上等同与

1
2
var method, _ := object.Get(name)
method.Call(object, argumentList...)
func (Object) Class
1
func (self  Object ) Class() string

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

<code>Object
Function
Array
String
Number
Boolean
Date
RegExp
</code>
func (Object) Get
1
func (self  Object ) Get(name string) (Value, error)

获得给定名字的属性

func (Object) Keys
1
func (self  Object ) Keys() []string

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值