Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

4.7 KB
Raw Blame
;;! component_model_threading = true

(component definition $Tester
  (core module $Memory
    (table (export "t") 1 funcref)
  )
  (core instance $memory (instantiate $Memory))

  (core type $ft (func (param i32)))
  (core func $thread.new-indirect (canon thread.new-indirect $ft (core table $memory "t")))
  (core func $thread.suspend (canon thread.suspend))
  (core func $thread.resume-later (canon thread.resume-later))
  (core func $thread.suspend-then-resume (canon thread.suspend-then-resume))
  (core func $thread.yield-then-resume (canon thread.yield-then-resume))
  (core func $thread.suspend-then-promote (canon thread.suspend-then-promote))
  (core func $thread.yield-then-promote (canon thread.yield-then-promote))

  (core module $DM
    (import "" "thread.new-indirect" (func $thread.new-indirect (param i32 i32) (result i32)))
    (import "" "thread.suspend" (func $thread.suspend (result i32)))
    (import "" "thread.resume-later" (func $thread.resume-later (param i32)))
    (import "" "thread.suspend-then-resume" (func $thread.suspend-then-resume (param i32) (result i32)))
    (import "" "thread.yield-then-resume" (func $thread.yield-then-resume (param i32) (result i32)))
    (import "" "thread.suspend-then-promote" (func $thread.suspend-then-promote (param i32) (result i32)))
    (import "" "thread.yield-then-promote" (func $thread.yield-then-promote (param i32) (result i32)))

    (func (export "noop"))
    (func (export "trap-calling-thread-new-indirect") (call $thread.new-indirect (i32.const 0) (i32.const 0)) drop)
    (func (export "trap-calling-thread-suspend") (call $thread.suspend) drop)
    (func (export "trap-calling-thread-resume-later") (call $thread.resume-later (i32.const 0)))
    (func (export "trap-calling-thread-suspend-then-resume") (call $thread.suspend-then-resume (i32.const 0)) drop)
    (func (export "trap-calling-thread-yield-then-resume") (call $thread.yield-then-resume (i32.const 0)) drop)
    (func (export "trap-calling-thread-suspend-then-promote") (call $thread.suspend-then-promote (i32.const 0)) drop)
    (func (export "trap-calling-thread-yield-then-promote") (call $thread.yield-then-promote (i32.const 0)) drop)
  )
  (core instance $dm (instantiate $DM (with "" (instance
    (export "thread.new-indirect" (func $thread.new-indirect))
    (export "thread.suspend" (func $thread.suspend))
    (export "thread.resume-later" (func $thread.resume-later))
    (export "thread.suspend-then-resume" (func $thread.suspend-then-resume))
    (export "thread.yield-then-resume" (func $thread.yield-then-resume))
    (export "thread.suspend-then-promote" (func $thread.suspend-then-promote))
    (export "thread.yield-then-promote" (func $thread.yield-then-promote))
  ))))
  (func (export "trap-calling-thread-new-indirect")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-new-indirect"))))
  (func (export "trap-calling-thread-suspend")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-suspend"))))
  (func (export "trap-calling-thread-resume-later")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-resume-later"))))
  (func (export "trap-calling-thread-suspend-then-resume")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-suspend-then-resume"))))
  (func (export "trap-calling-thread-yield-then-resume")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-yield-then-resume"))))
  (func (export "trap-calling-thread-suspend-then-promote")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-suspend-then-promote"))))
  (func (export "trap-calling-thread-yield-then-promote")
    (canon lift (core func $dm "noop")
      (post-return (core func $dm "trap-calling-thread-yield-then-promote"))))
)

(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-new-indirect") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-suspend") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-resume-later") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-suspend-then-resume") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-yield-then-resume") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-suspend-then-promote") "cannot leave component instance")
(component instance $i $Tester)
(assert_trap (invoke "trap-calling-thread-yield-then-promote") "cannot leave component instance")