Skip to content

Commit

Permalink
update readme, clean some minor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Feb 22, 2023
1 parent 373453e commit 3da9829
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
`import "github.com/trealla-prolog/go/trealla"`

Prolog interface for Go using [Trealla Prolog](https://github.com/trealla-prolog/trealla) and [Wasmer](https://github.com/wasmerio/wasmer-go).
It's pretty fast. Not as fast as native Trealla, but pretty dang fast (2-5x slower than native).
It's pretty fast. Not as fast as native Trealla, but pretty dang fast (about 2x slower than native).

**Development Status**: beta 🤠

Expand All @@ -26,7 +26,6 @@ go.dev is confused about this and will pull a very old version if you try to `go
This library uses WebAssembly to run Trealla, executing Prolog queries in an isolated environment.

```go

import "github.com/trealla-prolog/go/trealla"

func main() {
Expand Down Expand Up @@ -124,10 +123,23 @@ These additional predicates are built in:
- `crypto_data_hash/3`
- `http_consult/1`

## WASM Binary
## WASM binary

This library embeds the Trealla WebAssembly binary in itself, so you can use it without any external dependencies.
The binaries are currently sourced from [guregu/trealla](https://github.com/guregu/trealla), also [available from WAPM](https://wapm.io/guregu/trealla).
The binaries are currently sourced from [guregu/trealla](https://github.com/guregu/trealla).

### Building the WASM binary

If you'd like to build `libtpl.wasm` yourself:

```bash
# The submodule in src/trealla points to the current version
git submodule update --init --recursive
# Use the Makefile in the root of this project
make clean wasm
# Run the tests and benchmark to make sure it works
go test -v ./trealla -bench=.
```

## Thanks

Expand Down
12 changes: 5 additions & 7 deletions trealla/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func (pl *prolog) host_call() *wasmer.Function {
wasmer.NewFunctionType(
wasmer.NewValueTypes(wasmer.I32, wasmer.I32, wasmer.I32, wasmer.I32, wasmer.I32),
wasmer.NewValueTypes(wasmer.I32),
),
pl, hostCall)
), pl, hostCall)
}

func (pl *prolog) host_resume() *wasmer.Function {
Expand All @@ -50,7 +49,6 @@ func (pl *prolog) host_resume() *wasmer.Function {

func hostCall(env any, args []wasmer.Value) ([]wasmer.Value, error) {
pl := env.(*prolog)
_ = pl
subquery := args[0].I32()
msgptr := args[1].I32()
msgsize := args[2].I32()
Expand Down Expand Up @@ -84,7 +82,7 @@ func hostCall(env any, args []wasmer.Value) ([]wasmer.Value, error) {
if err := reply(expr.String()); err != nil {
return nil, err
}
return []wasmer.Value{wasm_true}, nil
return []wasmer.Value{wasmTrue}, nil
}

proc, ok := pl.procs[goal.Indicator()]
Expand All @@ -97,7 +95,7 @@ func hostCall(env any, args []wasmer.Value) ([]wasmer.Value, error) {
if err := reply(expr.String()); err != nil {
return nil, err
}
return []wasmer.Value{wasm_true}, nil
return []wasmer.Value{wasmTrue}, nil
}

locked := &lockedProlog{prolog: pl}
Expand All @@ -110,9 +108,9 @@ func hostCall(env any, args []wasmer.Value) ([]wasmer.Value, error) {
if err := reply(expr); err != nil {
return nil, err
}
return []wasmer.Value{wasm_true}, nil
return []wasmer.Value{wasmTrue}, nil
}

func hostResume(_ any, args []wasmer.Value) ([]wasmer.Value, error) {
return []wasmer.Value{wasm_false}, nil
return []wasmer.Value{wasmFalse}, nil
}
3 changes: 1 addition & 2 deletions trealla/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func crypto_data_hash_3(pl Prolog, _ Subquery, goal Term) Term {
return typeError("chars", data, piTerm("crypto_data_hash", 3))
}
switch hash.(type) {
case Variable: // ok
case string: // ok
case Variable, string: // ok
default:
return typeError("chars", hash, piTerm("crypto_data_hash", 3))
}
Expand Down
4 changes: 2 additions & 2 deletions trealla/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ func init() {

var (
// wasm_null = wasmer.NewI32(0)
wasm_false = wasmer.NewI32(0)
wasm_true = wasmer.NewI32(1)
wasmFalse = wasmer.NewI32(0)
wasmTrue = wasmer.NewI32(1)
)

0 comments on commit 3da9829

Please sign in to comment.