Skip to content

Commit

Permalink
Merge pull request #25 from d5/scriptctx
Browse files Browse the repository at this point in the history
Add Context to script run
  • Loading branch information
d5 committed Jan 20, 2019
2 parents 47418fd + 880a281 commit 0016872
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 97 deletions.
26 changes: 14 additions & 12 deletions README.md
Expand Up @@ -22,23 +22,25 @@ Tengo is [fast](#benchmark) as it's compiled to bytecode and executed on stack-b
- Garbage collected _(thanks to Go runtime)_
- Easily extensible using customizable types
- Written in pure Go _(no CGO, no external dependencies)_
- Executable as a [standalone language](https://github.com/d5/tengo#tengo-as-a-standalone-language) _(without writing any Go code)_
- Executable as a standalone language

## Benchmark

| | fib(35) | fibt(35) | Type |
| :--- | ---: | ---: | :---: |
| Go | `59ms` | `4ms` | Go (native) |
| [**Tengo**](https://github.com/d5/tengo) | `4,809ms` | `5ms` | VM on Go |
| Lua | `1,752ms` | `3ms` | Lua (native) |
| [go-lua](https://github.com/Shopify/go-lua) | `5,236ms` | `5ms` | Lua VM on Go |
| [GopherLua](https://github.com/yuin/gopher-lua) | `5,558ms` | `5ms` | Lua VM on Go |
| Python | `3,132ms` | `28ms` | Python (native) |
| [starlark-go](https://github.com/google/starlark-go) | `16,789ms` | `5ms` | Python-like Interpreter on Go |
| [otto](https://github.com/robertkrimen/otto) | `85,765ms` | `22ms` | JS Interpreter on Go |
| [Anko](https://github.com/mattn/anko) | `99,235ms` | `24ms` | Interpreter on Go |

[fib(35)](https://github.com/d5/tengobench/blob/master/code/fib.tengo) is a function to compute 35th Fibonacci number, and, [fibt(35)](https://github.com/d5/tengobench/blob/master/code/fibtc.tengo) is the [tail-call](https://en.wikipedia.org/wiki/Tail_call) version of the same function. You can see all the code used for this test in [tengobench](https://github.com/d5/tengobench).
| Go | `67ms` | `4ms` | Go (native) |
| [**Tengo**](https://github.com/d5/tengo) | `4,390ms` | `5ms` | VM on Go |
| Lua | `1,804ms` | `3ms` | Lua (native) |
| [go-lua](https://github.com/Shopify/go-lua) | `5,114ms` | `4ms` | Lua VM on Go |
| [GopherLua](https://github.com/yuin/gopher-lua) | `5,679ms` | `5ms` | Lua VM on Go |
| Python | `2,853ms` | `25ms` | Python (native) |
| [starlark-go](https://github.com/google/starlark-go) | `16,725ms` | `5ms` | Python-like Interpreter on Go |
| [otto](https://github.com/robertkrimen/otto) | `88,148ms` | `21ms` | JS Interpreter on Go |
| [Anko](https://github.com/mattn/anko) | `107,968ms` | `22ms` | Interpreter on Go |

[fib(35)](https://github.com/d5/tengobench/blob/master/code/fib.tengo) is a function to compute 35th Fibonacci number, and, [fibt(35)](https://github.com/d5/tengobench/blob/master/code/fibtc.tengo) is the [tail-call](https://en.wikipedia.org/wiki/Tail_call) version of the same function.

_Please note that **Go** case does not read the source code from a local file, while all other cases do. All shell commands and the source code used in this benchmarking is available [here](https://github.com/d5/tengobench)._

## Tengo Syntax in 5 Minutes

Expand Down
14 changes: 2 additions & 12 deletions compiler/opcodes.go
Expand Up @@ -176,23 +176,13 @@ func ReadOperands(numOperands []int, ins []byte) (operands []int, offset int) {
for _, width := range numOperands {
switch width {
case 1:
operands = append(operands, int(ReadUint8(ins[offset:])))
operands = append(operands, int(ins[offset]))
case 2:
operands = append(operands, int(ReadUint16(ins[offset:])))
operands = append(operands, int(ins[offset+1])|int(ins[offset])<<8)
}

offset += width
}

return
}

// ReadUint16 reads uint16 from the byte slice.
func ReadUint16(b []byte) uint16 {
return uint16(b[1]) | uint16(b[0])<<8
}

// ReadUint8 reads uint8 from the byte slice.
func ReadUint8(b []byte) uint8 {
return uint8(b[0])
}

0 comments on commit 0016872

Please sign in to comment.