| 1 |
|
| 2 |
|
| 3 |
package internal |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"compress/gzip" |
| 8 |
"context" |
| 9 |
_ "embed" |
| 10 |
"encoding/binary" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"io" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"runtime" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
"sync/atomic" |
| 20 |
"unsafe" |
| 21 |
|
| 22 |
wazero "github.com/tetratelabs/wazero" |
| 23 |
"github.com/tetratelabs/wazero/api" |
| 24 |
"github.com/tetratelabs/wazero/experimental" |
| 25 |
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" |
| 26 |
"github.com/wasilibs/wazero-helpers/allocator" |
| 27 |
) |
| 28 |
|
| 29 |
var errFailedRead = errors.New("failed to read from wasm memory") |
| 30 |
|
| 31 |
|
| 32 |
var libre2Gz []byte |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
var memoryWasm []byte |
| 38 |
|
| 39 |
var ( |
| 40 |
wasmRT wazero.Runtime |
| 41 |
wasmCompiled wazero.CompiledModule |
| 42 |
wasmMemory api.Memory |
| 43 |
rootMod api.Module |
| 44 |
|
| 45 |
wasmInitOnce sync.Once |
| 46 |
modPool []*childModule |
| 47 |
modPoolMu sync.Mutex |
| 48 |
modCreateMu sync.Mutex |
| 49 |
) |
| 50 |
|
| 51 |
type libre2ABI struct { |
| 52 |
cre2New lazyFunction |
| 53 |
cre2Delete lazyFunction |
| 54 |
cre2Match lazyFunction |
| 55 |
cre2NumCapturingGroups lazyFunction |
| 56 |
cre2ErrorCode lazyFunction |
| 57 |
cre2ErrorArg lazyFunction |
| 58 |
cre2NamedGroupsIterNew lazyFunction |
| 59 |
cre2NamedGroupsIterNext lazyFunction |
| 60 |
cre2NamedGroupsIterDelete lazyFunction |
| 61 |
cre2OptNew lazyFunction |
| 62 |
cre2OptDelete lazyFunction |
| 63 |
cre2OptSetLongestMatch lazyFunction |
| 64 |
cre2OptSetPosixSyntax lazyFunction |
| 65 |
cre2OptSetCaseSensitive lazyFunction |
| 66 |
cre2OptSetLatin1Encoding lazyFunction |
| 67 |
cre2OptSetMaxMem lazyFunction |
| 68 |
|
| 69 |
cre2SetNew lazyFunction |
| 70 |
cre2SetAdd lazyFunction |
| 71 |
cre2SetCompile lazyFunction |
| 72 |
cre2SetMatch lazyFunction |
| 73 |
cre2SetDelete lazyFunction |
| 74 |
|
| 75 |
malloc lazyFunction |
| 76 |
free lazyFunction |
| 77 |
} |
| 78 |
|
| 79 |
type wasmPtr uint32 |
| 80 |
|
| 81 |
var nilWasmPtr = wasmPtr(0) |
| 82 |
|
| 83 |
var prevTID uint32 |
| 84 |
|
| 85 |
type childModule struct { |
| 86 |
mod api.Module |
| 87 |
tlsBasePtr uint32 |
| 88 |
functions map[string]api.Function |
| 89 |
} |
| 90 |
|
| 91 |
func createChildModule(ctx context.Context, rt wazero.Runtime, root api.Module) *childModule { |
| 92 |
|
| 93 |
stackPointer := root.ExportedGlobal("__stack_pointer").Get() |
| 94 |
tlsBase := root.ExportedGlobal("__tls_base").Get() |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
size := stackPointer - tlsBase |
| 99 |
|
| 100 |
malloc := root.ExportedFunction("malloc") |
| 101 |
|
| 102 |
|
| 103 |
res, err := malloc.Call(ctx, size) |
| 104 |
if err != nil { |
| 105 |
panic(err) |
| 106 |
} |
| 107 |
ptr := uint32(res[0]) |
| 108 |
|
| 109 |
child, err := rt.InstantiateModule(ctx, wasmCompiled, wazero.NewModuleConfig().WithSysNanotime().WithSysWalltime().WithSysNanosleep().WithStdout(os.Stdout).WithStderr(os.Stderr). |
| 110 |
|
| 111 |
WithStartFunctions(). |
| 112 |
WithName("")) |
| 113 |
if err != nil { |
| 114 |
panic(err) |
| 115 |
} |
| 116 |
initTLS := child.ExportedFunction("__wasm_init_tls") |
| 117 |
if _, err := initTLS.Call(ctx, uint64(ptr)); err != nil { |
| 118 |
panic(err) |
| 119 |
} |
| 120 |
|
| 121 |
tid := atomic.AddUint32(&prevTID, 1) |
| 122 |
root.Memory().WriteUint32Le(ptr, ptr) |
| 123 |
root.Memory().WriteUint32Le(ptr+20, tid) |
| 124 |
if mg, ok := child.ExportedGlobal("__stack_pointer").(api.MutableGlobal); ok { |
| 125 |
mg.Set(uint64(ptr) + size) |
| 126 |
} |
| 127 |
|
| 128 |
ret := &childModule{ |
| 129 |
mod: child, |
| 130 |
tlsBasePtr: ptr, |
| 131 |
functions: map[string]api.Function{}, |
| 132 |
} |
| 133 |
runtime.SetFinalizer(ret, func(obj interface{}) { |
| 134 |
if cm, ok := obj.(*childModule); ok { |
| 135 |
free := cm.mod.ExportedFunction("free") |
| 136 |
if _, err := free.Call(ctx, uint64(cm.tlsBasePtr)); err != nil { |
| 137 |
panic(err) |
| 138 |
} |
| 139 |
_ = cm.mod.Close(context.Background()) |
| 140 |
} |
| 141 |
}) |
| 142 |
return ret |
| 143 |
} |
| 144 |
|
| 145 |
func getChildModule(ctx context.Context) *childModule { |
| 146 |
wasmInitOnce.Do(func() { |
| 147 |
initWASM(ctx) |
| 148 |
}) |
| 149 |
if cm := popChildModule(); cm != nil { |
| 150 |
return cm |
| 151 |
} |
| 152 |
|
| 153 |
modCreateMu.Lock() |
| 154 |
defer modCreateMu.Unlock() |
| 155 |
if cm := popChildModule(); cm != nil { |
| 156 |
return cm |
| 157 |
} |
| 158 |
return createChildModule(ctx, wasmRT, rootMod) |
| 159 |
} |
| 160 |
|
| 161 |
func putChildModule(cm *childModule) { |
| 162 |
modPoolMu.Lock() |
| 163 |
modPool = append(modPool, cm) |
| 164 |
modPoolMu.Unlock() |
| 165 |
} |
| 166 |
|
| 167 |
func popChildModule() *childModule { |
| 168 |
modPoolMu.Lock() |
| 169 |
defer modPoolMu.Unlock() |
| 170 |
n := len(modPool) |
| 171 |
if n == 0 { |
| 172 |
return nil |
| 173 |
} |
| 174 |
cm := modPool[n-1] |
| 175 |
modPool[n-1] = nil |
| 176 |
modPool = modPool[:n-1] |
| 177 |
return cm |
| 178 |
} |
| 179 |
|
| 180 |
func initWASM(ctx context.Context) { |
| 181 |
ctx = experimental.WithMemoryAllocator(ctx, allocator.NewNonMoving()) |
| 182 |
|
| 183 |
rtCfg := wazero.NewRuntimeConfig().WithCoreFeatures(api.CoreFeaturesV2 | experimental.CoreFeaturesThreads) |
| 184 |
uc, err := os.UserCacheDir() |
| 185 |
if err == nil { |
| 186 |
cache, err := wazero.NewCompilationCacheWithDir(filepath.Join(uc, "com.github.wasilibs")) |
| 187 |
if err == nil { |
| 188 |
rtCfg = rtCfg.WithCompilationCache(cache) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
maxPages := defaultMaxPages |
| 193 |
if unsafe.Sizeof(uintptr(0)) < 8 { |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
maxPagesLimit := uint32(65536 / 4) |
| 198 |
if maxPages > maxPagesLimit { |
| 199 |
maxPages = maxPagesLimit |
| 200 |
} |
| 201 |
} |
| 202 |
rtCfg = rtCfg.WithMemoryLimitPages(maxPages) |
| 203 |
|
| 204 |
rt := wazero.NewRuntimeWithConfig(ctx, rtCfg) |
| 205 |
|
| 206 |
wasi_snapshot_preview1.MustInstantiate(ctx, rt) |
| 207 |
|
| 208 |
if _, err := rt.InstantiateWithConfig(ctx, memoryWasm, wazero.NewModuleConfig().WithName("env")); err != nil { |
| 209 |
panic(err) |
| 210 |
} |
| 211 |
|
| 212 |
gz, err := gzip.NewReader(bytes.NewReader(libre2Gz)) |
| 213 |
if err != nil { |
| 214 |
panic(err) |
| 215 |
} |
| 216 |
libre2, err := io.ReadAll(gz) |
| 217 |
if err != nil { |
| 218 |
panic(err) |
| 219 |
} |
| 220 |
|
| 221 |
code, err := rt.CompileModule(ctx, libre2) |
| 222 |
if err != nil { |
| 223 |
panic(err) |
| 224 |
} |
| 225 |
wasmCompiled = code |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
var stdout, stderr io.Writer = os.Stdout, os.Stderr |
| 231 |
|
| 232 |
if _, err := os.Stdout.Stat(); err != nil { |
| 233 |
stdout = io.Discard |
| 234 |
} |
| 235 |
if _, err := os.Stderr.Stat(); err != nil { |
| 236 |
stderr = io.Discard |
| 237 |
} |
| 238 |
|
| 239 |
wasmRT = rt |
| 240 |
root, err := wasmRT.InstantiateModule(ctx, wasmCompiled, wazero.NewModuleConfig().WithSysWalltime().WithSysNanotime().WithSysNanosleep().WithStdout(stdout).WithStderr(stderr).WithStartFunctions("_initialize").WithName("")) |
| 241 |
if err != nil { |
| 242 |
panic(err) |
| 243 |
} |
| 244 |
wasmMemory = root.Memory() |
| 245 |
rootMod = root |
| 246 |
} |
| 247 |
|
| 248 |
func newABI() *libre2ABI { |
| 249 |
abi := &libre2ABI{ |
| 250 |
cre2New: newLazyFunction("cre2_new"), |
| 251 |
cre2Delete: newLazyFunction("cre2_delete"), |
| 252 |
cre2Match: newLazyFunction("cre2_match"), |
| 253 |
cre2NumCapturingGroups: newLazyFunction("cre2_num_capturing_groups"), |
| 254 |
cre2ErrorCode: newLazyFunction("cre2_error_code"), |
| 255 |
cre2ErrorArg: newLazyFunction("cre2_error_arg"), |
| 256 |
cre2NamedGroupsIterNew: newLazyFunction("cre2_named_groups_iter_new"), |
| 257 |
cre2NamedGroupsIterNext: newLazyFunction("cre2_named_groups_iter_next"), |
| 258 |
cre2NamedGroupsIterDelete: newLazyFunction("cre2_named_groups_iter_delete"), |
| 259 |
cre2OptNew: newLazyFunction("cre2_opt_new"), |
| 260 |
cre2OptDelete: newLazyFunction("cre2_opt_delete"), |
| 261 |
cre2OptSetLongestMatch: newLazyFunction("cre2_opt_set_longest_match"), |
| 262 |
cre2OptSetPosixSyntax: newLazyFunction("cre2_opt_set_posix_syntax"), |
| 263 |
cre2OptSetCaseSensitive: newLazyFunction("cre2_opt_set_case_sensitive"), |
| 264 |
cre2OptSetLatin1Encoding: newLazyFunction("cre2_opt_set_latin1_encoding"), |
| 265 |
cre2OptSetMaxMem: newLazyFunction("cre2_opt_set_max_mem"), |
| 266 |
cre2SetNew: newLazyFunction("cre2_set_new"), |
| 267 |
cre2SetAdd: newLazyFunction("cre2_set_add"), |
| 268 |
cre2SetCompile: newLazyFunction("cre2_set_compile"), |
| 269 |
cre2SetMatch: newLazyFunction("cre2_set_match"), |
| 270 |
cre2SetDelete: newLazyFunction("cre2_set_delete"), |
| 271 |
malloc: newLazyFunction("malloc"), |
| 272 |
free: newLazyFunction("free"), |
| 273 |
} |
| 274 |
|
| 275 |
return abi |
| 276 |
} |
| 277 |
|
| 278 |
func (abi *libre2ABI) startOperation(memorySize int) allocation { |
| 279 |
return abi.reserve(uint32(memorySize)) |
| 280 |
} |
| 281 |
|
| 282 |
func (abi *libre2ABI) endOperation(a allocation) { |
| 283 |
a.free() |
| 284 |
} |
| 285 |
|
| 286 |
func newRE(abi *libre2ABI, pattern cString, opts CompileOptions) wasmPtr { |
| 287 |
ctx := context.Background() |
| 288 |
optPtr := uint32(0) |
| 289 |
res, err := abi.cre2OptNew.Call0(ctx) |
| 290 |
if err != nil { |
| 291 |
panic(err) |
| 292 |
} |
| 293 |
optPtr = uint32(res) |
| 294 |
defer func() { |
| 295 |
if _, err := abi.cre2OptDelete.Call1(ctx, uint64(optPtr)); err != nil { |
| 296 |
panic(err) |
| 297 |
} |
| 298 |
}() |
| 299 |
|
| 300 |
_, err = abi.cre2OptSetMaxMem.Call2(ctx, uint64(optPtr), uint64(maxSize)) |
| 301 |
if err != nil { |
| 302 |
panic(err) |
| 303 |
} |
| 304 |
|
| 305 |
if opts.Longest { |
| 306 |
_, err = abi.cre2OptSetLongestMatch.Call2(ctx, uint64(optPtr), 1) |
| 307 |
if err != nil { |
| 308 |
panic(err) |
| 309 |
} |
| 310 |
} |
| 311 |
if opts.Posix { |
| 312 |
_, err = abi.cre2OptSetPosixSyntax.Call2(ctx, uint64(optPtr), 1) |
| 313 |
if err != nil { |
| 314 |
panic(err) |
| 315 |
} |
| 316 |
} |
| 317 |
if opts.CaseInsensitive { |
| 318 |
_, err = abi.cre2OptSetCaseSensitive.Call2(ctx, uint64(optPtr), 0) |
| 319 |
if err != nil { |
| 320 |
panic(err) |
| 321 |
} |
| 322 |
} |
| 323 |
if opts.Latin1 { |
| 324 |
_, err = abi.cre2OptSetLatin1Encoding.Call1(ctx, uint64(optPtr)) |
| 325 |
if err != nil { |
| 326 |
panic(err) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
res, err = abi.cre2New.Call3(ctx, uint64(pattern.ptr), uint64(pattern.length), uint64(optPtr)) |
| 331 |
if err != nil { |
| 332 |
panic(err) |
| 333 |
} |
| 334 |
return wasmPtr(res) |
| 335 |
} |
| 336 |
|
| 337 |
func reError(abi *libre2ABI, rePtr wasmPtr) (int, string) { |
| 338 |
ctx := context.Background() |
| 339 |
res, err := abi.cre2ErrorCode.Call1(ctx, uint64(rePtr)) |
| 340 |
if err != nil { |
| 341 |
panic(err) |
| 342 |
} |
| 343 |
code := int(res) |
| 344 |
if code == 0 { |
| 345 |
return 0, "" |
| 346 |
} |
| 347 |
|
| 348 |
res, err = abi.cre2ErrorArg.Call1(ctx, uint64(rePtr)) |
| 349 |
if err != nil { |
| 350 |
panic(err) |
| 351 |
} |
| 352 |
msg := copyCString(wasmPtr(res)) |
| 353 |
return code, msg |
| 354 |
} |
| 355 |
|
| 356 |
func numCapturingGroups(abi *libre2ABI, rePtr wasmPtr) int { |
| 357 |
ctx := context.Background() |
| 358 |
res, err := abi.cre2NumCapturingGroups.Call1(ctx, uint64(rePtr)) |
| 359 |
if err != nil { |
| 360 |
panic(err) |
| 361 |
} |
| 362 |
return int(res) |
| 363 |
} |
| 364 |
|
| 365 |
func deleteRE(abi *libre2ABI, rePtr wasmPtr) { |
| 366 |
ctx := context.Background() |
| 367 |
if _, err := abi.cre2Delete.Call1(ctx, uint64(rePtr)); err != nil { |
| 368 |
panic(err) |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func release(re *Regexp) { |
| 373 |
deleteRE(re.abi, re.ptr) |
| 374 |
} |
| 375 |
|
| 376 |
func match(re *Regexp, s cString, matchesPtr wasmPtr, nMatches uint32) bool { |
| 377 |
ctx := context.Background() |
| 378 |
res, err := re.abi.cre2Match.Call8(ctx, uint64(re.ptr), uint64(s.ptr), uint64(s.length), 0, uint64(s.length), 0, uint64(matchesPtr), uint64(nMatches)) |
| 379 |
if err != nil { |
| 380 |
panic(err) |
| 381 |
} |
| 382 |
|
| 383 |
return res == 1 |
| 384 |
} |
| 385 |
|
| 386 |
func matchFrom(re *Regexp, s cString, startPos int, matchesPtr wasmPtr, nMatches uint32) bool { |
| 387 |
ctx := context.Background() |
| 388 |
res, err := re.abi.cre2Match.Call8(ctx, uint64(re.ptr), uint64(s.ptr), uint64(s.length), uint64(startPos), uint64(s.length), 0, uint64(matchesPtr), uint64(nMatches)) |
| 389 |
if err != nil { |
| 390 |
panic(err) |
| 391 |
} |
| 392 |
|
| 393 |
return res == 1 |
| 394 |
} |
| 395 |
|
| 396 |
func readMatch(alloc *allocation, cs cString, matchPtr wasmPtr, dstCap []int) []int { |
| 397 |
matchBuf := alloc.read(matchPtr, 8) |
| 398 |
subStrPtr := binary.LittleEndian.Uint32(matchBuf) |
| 399 |
sLen := binary.LittleEndian.Uint32(matchBuf[4:]) |
| 400 |
sIdx := subStrPtr - uint32(cs.ptr) |
| 401 |
|
| 402 |
return append(dstCap, int(sIdx), int(sIdx+sLen)) |
| 403 |
} |
| 404 |
|
| 405 |
func readMatches(alloc *allocation, cs cString, matchesPtr wasmPtr, n int, deliver func([]int) bool) { |
| 406 |
var dstCap [2]int |
| 407 |
|
| 408 |
matchesBuf := alloc.read(matchesPtr, 8*n) |
| 409 |
for i := range n { |
| 410 |
subStrPtr := binary.LittleEndian.Uint32(matchesBuf[8*i:]) |
| 411 |
if subStrPtr == 0 { |
| 412 |
if !deliver(append(dstCap[:0], -1, -1)) { |
| 413 |
break |
| 414 |
} |
| 415 |
continue |
| 416 |
} |
| 417 |
sLen := binary.LittleEndian.Uint32(matchesBuf[8*i+4:]) |
| 418 |
sIdx := subStrPtr - uint32(cs.ptr) |
| 419 |
if !deliver(append(dstCap[:0], int(sIdx), int(sIdx+sLen))) { |
| 420 |
break |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
func namedGroupsIter(abi *libre2ABI, rePtr wasmPtr) wasmPtr { |
| 426 |
ctx := context.Background() |
| 427 |
|
| 428 |
res, err := abi.cre2NamedGroupsIterNew.Call1(ctx, uint64(rePtr)) |
| 429 |
if err != nil { |
| 430 |
panic(err) |
| 431 |
} |
| 432 |
|
| 433 |
return wasmPtr(res) |
| 434 |
} |
| 435 |
|
| 436 |
func namedGroupsIterNext(abi *libre2ABI, iterPtr wasmPtr) (string, int, bool) { |
| 437 |
ctx := context.Background() |
| 438 |
|
| 439 |
|
| 440 |
ptrs := malloc(abi, 8) |
| 441 |
defer free(abi, ptrs) |
| 442 |
namePtrPtr := ptrs |
| 443 |
indexPtr := namePtrPtr + 4 |
| 444 |
|
| 445 |
res, err := abi.cre2NamedGroupsIterNext.Call3(ctx, uint64(iterPtr), uint64(namePtrPtr), uint64(indexPtr)) |
| 446 |
if err != nil { |
| 447 |
panic(err) |
| 448 |
} |
| 449 |
|
| 450 |
if res == 0 { |
| 451 |
return "", 0, false |
| 452 |
} |
| 453 |
|
| 454 |
namePtr, ok := wasmMemory.ReadUint32Le(uint32(namePtrPtr)) |
| 455 |
if !ok { |
| 456 |
panic(errFailedRead) |
| 457 |
} |
| 458 |
|
| 459 |
name := copyCString(wasmPtr(namePtr)) |
| 460 |
|
| 461 |
index, ok := wasmMemory.ReadUint32Le(uint32(indexPtr)) |
| 462 |
if !ok { |
| 463 |
panic(errFailedRead) |
| 464 |
} |
| 465 |
|
| 466 |
return name, int(index), true |
| 467 |
} |
| 468 |
|
| 469 |
func namedGroupsIterDelete(abi *libre2ABI, iterPtr wasmPtr) { |
| 470 |
ctx := context.Background() |
| 471 |
|
| 472 |
_, err := abi.cre2NamedGroupsIterDelete.Call1(ctx, uint64(iterPtr)) |
| 473 |
if err != nil { |
| 474 |
panic(err) |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
func newSet(abi *libre2ABI, opts CompileOptions) wasmPtr { |
| 479 |
ctx := context.Background() |
| 480 |
optPtr := uint32(0) |
| 481 |
res, err := abi.cre2OptNew.Call0(ctx) |
| 482 |
if err != nil { |
| 483 |
panic(err) |
| 484 |
} |
| 485 |
optPtr = uint32(res) |
| 486 |
defer func() { |
| 487 |
if _, err := abi.cre2OptDelete.Call1(ctx, uint64(optPtr)); err != nil { |
| 488 |
panic(err) |
| 489 |
} |
| 490 |
}() |
| 491 |
|
| 492 |
_, err = abi.cre2OptSetMaxMem.Call2(ctx, uint64(optPtr), uint64(maxSize)) |
| 493 |
if err != nil { |
| 494 |
panic(err) |
| 495 |
} |
| 496 |
|
| 497 |
if opts.Longest { |
| 498 |
_, err = abi.cre2OptSetLongestMatch.Call2(ctx, uint64(optPtr), 1) |
| 499 |
if err != nil { |
| 500 |
panic(err) |
| 501 |
} |
| 502 |
} |
| 503 |
if opts.Posix { |
| 504 |
_, err = abi.cre2OptSetPosixSyntax.Call2(ctx, uint64(optPtr), 1) |
| 505 |
if err != nil { |
| 506 |
panic(err) |
| 507 |
} |
| 508 |
} |
| 509 |
if opts.CaseInsensitive { |
| 510 |
_, err = abi.cre2OptSetCaseSensitive.Call2(ctx, uint64(optPtr), 0) |
| 511 |
if err != nil { |
| 512 |
panic(err) |
| 513 |
} |
| 514 |
} |
| 515 |
if opts.Latin1 { |
| 516 |
_, err = abi.cre2OptSetLatin1Encoding.Call1(ctx, uint64(optPtr)) |
| 517 |
if err != nil { |
| 518 |
panic(err) |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
res, err = abi.cre2SetNew.Call2(ctx, uint64(optPtr), 0) |
| 523 |
if err != nil { |
| 524 |
panic(err) |
| 525 |
} |
| 526 |
return wasmPtr(res) |
| 527 |
} |
| 528 |
|
| 529 |
func setAdd(set *Set, s cString) string { |
| 530 |
ctx := context.Background() |
| 531 |
res, err := set.abi.cre2SetAdd.Call3(ctx, uint64(set.ptr), uint64(s.ptr), uint64(s.length)) |
| 532 |
if err != nil { |
| 533 |
panic(err) |
| 534 |
} |
| 535 |
if res == 0 { |
| 536 |
return unknownCompileError |
| 537 |
} |
| 538 |
msgPtr := wasmPtr(res) |
| 539 |
msg := copyCString(msgPtr) |
| 540 |
if msg != "ok" { |
| 541 |
free(set.abi, msgPtr) |
| 542 |
return "error parsing regexp: " + msg |
| 543 |
} |
| 544 |
return "" |
| 545 |
} |
| 546 |
|
| 547 |
func setCompile(set *Set) int32 { |
| 548 |
ctx := context.Background() |
| 549 |
res, err := set.abi.cre2SetCompile.Call1(ctx, uint64(set.ptr)) |
| 550 |
if err != nil { |
| 551 |
panic(err) |
| 552 |
} |
| 553 |
return int32(res) |
| 554 |
} |
| 555 |
|
| 556 |
func setMatch(set *Set, cs cString, matchedPtr wasmPtr, nMatch int) int { |
| 557 |
ctx := context.Background() |
| 558 |
res, err := set.abi.cre2SetMatch.Call5(ctx, uint64(set.ptr), uint64(cs.ptr), uint64(cs.length), uint64(matchedPtr), uint64(nMatch)) |
| 559 |
if err != nil { |
| 560 |
panic(err) |
| 561 |
} |
| 562 |
return int(res) |
| 563 |
} |
| 564 |
|
| 565 |
func deleteSet(abi *libre2ABI, setPtr wasmPtr) { |
| 566 |
ctx := context.Background() |
| 567 |
_, err := abi.cre2SetDelete.Call1(ctx, uint64(setPtr)) |
| 568 |
if err != nil { |
| 569 |
panic(err) |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
type cString struct { |
| 574 |
ptr wasmPtr |
| 575 |
length int |
| 576 |
} |
| 577 |
|
| 578 |
type cStringArray struct { |
| 579 |
ptr wasmPtr |
| 580 |
} |
| 581 |
|
| 582 |
func (a cStringArray) free() { |
| 583 |
|
| 584 |
} |
| 585 |
|
| 586 |
func malloc(abi *libre2ABI, size uint32) wasmPtr { |
| 587 |
if res, err := abi.malloc.Call1(context.Background(), uint64(size)); err != nil { |
| 588 |
panic(err) |
| 589 |
} else { |
| 590 |
return wasmPtr(res) |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
func free(abi *libre2ABI, ptr wasmPtr) { |
| 595 |
if _, err := abi.free.Call1(context.Background(), uint64(ptr)); err != nil { |
| 596 |
panic(err) |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
func copyCString(ptr wasmPtr) string { |
| 601 |
res := strings.Builder{} |
| 602 |
for { |
| 603 |
b, ok := wasmMemory.ReadByte(uint32(ptr)) |
| 604 |
if !ok { |
| 605 |
panic(errFailedRead) |
| 606 |
} |
| 607 |
if b == 0 { |
| 608 |
break |
| 609 |
} |
| 610 |
res.WriteByte(b) |
| 611 |
ptr++ |
| 612 |
} |
| 613 |
return res.String() |
| 614 |
} |
| 615 |
|
| 616 |
type allocation struct { |
| 617 |
size uint32 |
| 618 |
bufPtr wasmPtr |
| 619 |
nextIdx uint32 |
| 620 |
abi *libre2ABI |
| 621 |
} |
| 622 |
|
| 623 |
func (abi *libre2ABI) reserve(size uint32) allocation { |
| 624 |
ptr := malloc(abi, size) |
| 625 |
return allocation{ |
| 626 |
size: size, |
| 627 |
bufPtr: ptr, |
| 628 |
nextIdx: 0, |
| 629 |
abi: abi, |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
func (a *allocation) free() { |
| 634 |
free(a.abi, a.bufPtr) |
| 635 |
} |
| 636 |
|
| 637 |
func (a *allocation) allocate(size uint32) wasmPtr { |
| 638 |
if a.nextIdx+size > a.size { |
| 639 |
panic("not enough reserved shared memory") |
| 640 |
} |
| 641 |
|
| 642 |
ptr := uint32(a.bufPtr) + a.nextIdx |
| 643 |
a.nextIdx += size |
| 644 |
return wasmPtr(ptr) |
| 645 |
} |
| 646 |
|
| 647 |
func (a *allocation) read(ptr wasmPtr, size int) []byte { |
| 648 |
buf, ok := wasmMemory.Read(uint32(ptr), uint32(size)) |
| 649 |
if !ok { |
| 650 |
panic(errFailedRead) |
| 651 |
} |
| 652 |
return buf |
| 653 |
} |
| 654 |
|
| 655 |
func (a *allocation) write(b []byte) wasmPtr { |
| 656 |
ptr := a.allocate(uint32(len(b))) |
| 657 |
wasmMemory.Write(uint32(ptr), b) |
| 658 |
return ptr |
| 659 |
} |
| 660 |
|
| 661 |
func (a *allocation) writeString(s string) wasmPtr { |
| 662 |
ptr := a.allocate(uint32(len(s))) |
| 663 |
wasmMemory.WriteString(uint32(ptr), s) |
| 664 |
return ptr |
| 665 |
} |
| 666 |
|
| 667 |
func (a *allocation) newCString(s string) cString { |
| 668 |
ptr := a.writeString(s) |
| 669 |
return cString{ |
| 670 |
ptr: ptr, |
| 671 |
length: len(s), |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
func (a *allocation) newCStringFromBytes(s []byte) cString { |
| 676 |
ptr := a.write(s) |
| 677 |
return cString{ |
| 678 |
ptr: ptr, |
| 679 |
length: len(s), |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
func (a *allocation) newCStringArray(n int) cStringArray { |
| 684 |
ptr := a.allocate(uint32(n * 8)) |
| 685 |
return cStringArray{ptr: ptr} |
| 686 |
} |
| 687 |
|
| 688 |
type lazyFunction struct { |
| 689 |
name string |
| 690 |
} |
| 691 |
|
| 692 |
func newLazyFunction(name string) lazyFunction { |
| 693 |
return lazyFunction{name: name} |
| 694 |
} |
| 695 |
|
| 696 |
func (f *lazyFunction) Call0(ctx context.Context) (uint64, error) { |
| 697 |
var callStack [1]uint64 |
| 698 |
return f.callWithStack(ctx, callStack[:]) |
| 699 |
} |
| 700 |
|
| 701 |
func (f *lazyFunction) Call1(ctx context.Context, arg1 uint64) (uint64, error) { |
| 702 |
var callStack [1]uint64 |
| 703 |
callStack[0] = arg1 |
| 704 |
return f.callWithStack(ctx, callStack[:]) |
| 705 |
} |
| 706 |
|
| 707 |
func (f *lazyFunction) Call2(ctx context.Context, arg1 uint64, arg2 uint64) (uint64, error) { |
| 708 |
var callStack [2]uint64 |
| 709 |
callStack[0] = arg1 |
| 710 |
callStack[1] = arg2 |
| 711 |
return f.callWithStack(ctx, callStack[:]) |
| 712 |
} |
| 713 |
|
| 714 |
func (f *lazyFunction) Call3(ctx context.Context, arg1 uint64, arg2 uint64, arg3 uint64) (uint64, error) { |
| 715 |
var callStack [3]uint64 |
| 716 |
callStack[0] = arg1 |
| 717 |
callStack[1] = arg2 |
| 718 |
callStack[2] = arg3 |
| 719 |
return f.callWithStack(ctx, callStack[:]) |
| 720 |
} |
| 721 |
|
| 722 |
func (f *lazyFunction) Call5(ctx context.Context, arg1 uint64, arg2 uint64, arg3 uint64, arg4 uint64, arg5 uint64) (uint64, error) { |
| 723 |
var callStack [5]uint64 |
| 724 |
callStack[0] = arg1 |
| 725 |
callStack[1] = arg2 |
| 726 |
callStack[2] = arg3 |
| 727 |
callStack[3] = arg4 |
| 728 |
callStack[4] = arg5 |
| 729 |
return f.callWithStack(ctx, callStack[:]) |
| 730 |
} |
| 731 |
|
| 732 |
func (f *lazyFunction) Call8(ctx context.Context, arg1 uint64, arg2 uint64, arg3 uint64, arg4 uint64, arg5 uint64, arg6 uint64, arg7 uint64, arg8 uint64) (uint64, error) { |
| 733 |
var callStack [8]uint64 |
| 734 |
callStack[0] = arg1 |
| 735 |
callStack[1] = arg2 |
| 736 |
callStack[2] = arg3 |
| 737 |
callStack[3] = arg4 |
| 738 |
callStack[4] = arg5 |
| 739 |
callStack[5] = arg6 |
| 740 |
callStack[6] = arg7 |
| 741 |
callStack[7] = arg8 |
| 742 |
return f.callWithStack(ctx, callStack[:]) |
| 743 |
} |
| 744 |
|
| 745 |
func (f *lazyFunction) callWithStack(ctx context.Context, callStack []uint64) (uint64, error) { |
| 746 |
modH := getChildModule(ctx) |
| 747 |
defer putChildModule(modH) |
| 748 |
|
| 749 |
fun := modH.functions[f.name] |
| 750 |
if fun == nil { |
| 751 |
fun = modH.mod.ExportedFunction(f.name) |
| 752 |
modH.functions[f.name] = fun |
| 753 |
} |
| 754 |
|
| 755 |
if err := fun.CallWithStack(ctx, callStack); err != nil { |
| 756 |
return 0, fmt.Errorf("re2_wazero: calling function: %w", err) |
| 757 |
} |
| 758 |
return callStack[0], nil |
| 759 |
} |