Skip to content

Commit

Permalink
update trealla, with stricter module resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Nov 14, 2024
1 parent d7075e7 commit 832de18
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion trealla/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func Example() {
}

// start a new query
query := pl.Query(ctx, "member(X, [1, foo(bar), c]).")
query := pl.Query(ctx, "use_module(library(lists)), member(X, [1, foo(bar), c]).")
// calling Close is not necessary if you iterate through the whole query, but it doesn't hurt
defer query.Close()

Expand Down
2 changes: 1 addition & 1 deletion trealla/interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (pl *prolog) register(ctx context.Context, name string, arity int, proc Pre
pl.procs[pi.String()] = proc
vars := numbervars(arity)
head := functor.Of(vars...)
body := Atom("host_rpc").Of(head)
body := Atom(":").Of(Atom("wasm_generic"), Atom("host_rpc").Of(head))
clause := fmt.Sprintf(`%s :- %s.`, head.String(), body.String())
return pl.consultText(ctx, "user", clause)
}
Expand Down
Binary file modified trealla/libtpl.wasm
Binary file not shown.
17 changes: 13 additions & 4 deletions trealla/prolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,19 @@ type prolog struct {
spawning map[uint32]*query
limiter chan struct{}

ptr uint32
realloc wasmFunc
free wasmFunc
ptr uint32
// from stdlib
realloc wasmFunc
free wasmFunc
// from trealla.h
pl_consult wasmFunc
pl_capture wasmFunc
pl_capture_read wasmFunc
pl_capture_reset wasmFunc
pl_query wasmFunc
pl_redo wasmFunc
pl_done wasmFunc
pl_error wasmFunc

procs map[string]Predicate
coros map[int64]coroutine
Expand Down Expand Up @@ -104,7 +107,7 @@ func New(opts ...Option) (Prolog, error) {
}

func (pl *prolog) argv() []string {
args := []string{"tpl", "-g", "halt", "--ns"}
args := []string{"tpl", "--ns"}
if pl.library != "" {
args = append(args, "--library", pl.library)
}
Expand All @@ -130,6 +133,7 @@ func (pl *prolog) init(parent *prolog) error {
cfg := wazero.NewModuleConfig().WithName("").WithArgs(argv...).WithFSConfig(fs).
WithSysWalltime().WithSysNanotime().WithSysNanosleep().
WithOsyield(runtime.Gosched).
// WithStdout(os.Stdout).WithStderr(os.Stderr). // for debugging output capture
WithRandSource(rand.Reader)

// run once to initialize global interpreter
Expand Down Expand Up @@ -190,6 +194,11 @@ func (pl *prolog) init(parent *prolog) error {
return err
}

pl.pl_error, err = pl.function("pl_error")
if err != nil {
return err
}

pl.pl_consult, err = pl.function("pl_consult")
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion trealla/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func (q *query) Err() error {

func escapeQuery(query string) string {
query = queryEscaper.Replace(query)
return fmt.Sprintf(`js_ask(%s).`, escapeString(query))
return fmt.Sprintf(`wasm:js_ask(%s).`, escapeString(query))
}

// QueryOption is an optional parameter for queries.
Expand Down

0 comments on commit 832de18

Please sign in to comment.