Skip to content

Commit

Permalink
update quickjs library to not use libc and load modules manually.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 26, 2023
1 parent c6082e4 commit 4fe0cbc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"os"
"runtime"

"github.com/fiatjaf/quickjs-go"
Expand Down Expand Up @@ -139,10 +140,19 @@ func runAndGetResult(scriptPath scriptPath, makeArgs ...func(qjs *quickjs.Contex
}
qjs.Globals().Set("args", args.ToValue())

// register module
code, err := os.ReadFile(string(scriptPath))
if err != nil {
log.Warn().Err(err).Str("script", string(scriptPath)).Msg("couldn't read policy file")
return true, "couldn't read policy file"
}
qjs.RegisterModule(string(scriptPath), code)

// actually run it
val, err := qjs.Eval(`
import rejectEvent from './` + string(scriptPath) + `'
let msg = rejectEvent(...args)
____grab(msg)
____grab(msg) // this will also handle the case in which 'msg' is a promise
`)
defer val.Free()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.4
require (
github.com/fiatjaf/eventstore v0.2.11
github.com/fiatjaf/khatru v0.0.13
github.com/fiatjaf/quickjs-go v0.2.1
github.com/fiatjaf/quickjs-go v0.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/nbd-wtf/go-nostr v0.25.7
github.com/rs/zerolog v1.31.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/fiatjaf/eventstore v0.2.11 h1:hb3ImjJAX+aNvMd19sttbZD2PzgohRuZtOgVQLs
github.com/fiatjaf/eventstore v0.2.11/go.mod h1:Zx1XqwICh7RxxKLkgc0aXlVo298ABs4W5awP/1/bYYs=
github.com/fiatjaf/khatru v0.0.13 h1:yWKGnAvgNsxqG1OX0K60+yDd0nrcnGZrAPw14cgA+Tg=
github.com/fiatjaf/khatru v0.0.13/go.mod h1:ShdaRrzoZpCKjillsH7OS1O8xnxQcJP4bA5niBPUb/o=
github.com/fiatjaf/quickjs-go v0.2.1 h1:nTLusdLA9xRxnkha+xFLjKWP7LwmOBARZkNBa4TaL60=
github.com/fiatjaf/quickjs-go v0.2.1/go.mod h1:lYXCC+EmJ6YxXs128amkCmMXnimlO4dFCqY6fjwGC0M=
github.com/fiatjaf/quickjs-go v0.3.0 h1:wLvvummPLBrH3JDO4wHCC+uYVIDEHuUT2Jz1Ha65i3A=
github.com/fiatjaf/quickjs-go v0.3.0/go.mod h1:lYXCC+EmJ6YxXs128amkCmMXnimlO4dFCqY6fjwGC0M=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
Expand Down

0 comments on commit 4fe0cbc

Please sign in to comment.