|
1 | | -;; $D exports one function for each place in the spec where may_leave is |
2 | | -;; guarded to be true and ensures that the call traps before any other guard. |
3 | | -;; Since each trap tears down the instance, a fresh instance of $Tester is |
4 | | -;; created for each export call. |
| 1 | +;; Tests behavior of built-ins called from inside a post-return function. |
| 2 | + |
| 3 | +;; built-ins that trap when called from post-return: |
5 | 4 | (component definition $Tester |
6 | 5 | (component $C |
7 | 6 | (core module $CM |
|
247 | 246 | (assert_trap (invoke "trap-calling-future-drop-readable") "cannot leave component instance") |
248 | 247 | (component instance $i27 $Tester) |
249 | 248 | (assert_trap (invoke "trap-calling-future-drop-writable") "cannot leave component instance") |
| 249 | + |
| 250 | + |
| 251 | +;; built-ins that don't trap: |
| 252 | +(component |
| 253 | + (canon context.get i32 0 (core func $context.get)) |
| 254 | + (canon context.set i32 0 (core func $context.set)) |
| 255 | + (core module $CM |
| 256 | + (import "" "context.get" (func $context.get (result i32))) |
| 257 | + (import "" "context.set" (func $context.set (param i32))) |
| 258 | + (global $saved (mut i32) (i32.const 0)) |
| 259 | + |
| 260 | + (func (export "f") (result i32) |
| 261 | + (call $context.set (i32.const 42)) |
| 262 | + (i32.const 7) |
| 263 | + ) |
| 264 | + (func (export "f-pr") (param i32) |
| 265 | + (global.set $saved (call $context.get)) |
| 266 | + (call $context.set (i32.const 99)) |
| 267 | + ) |
| 268 | + (func (export "check") (result i32) |
| 269 | + (global.get $saved) |
| 270 | + ) |
| 271 | + ) |
| 272 | + (core instance $cm (instantiate $CM (with "" (instance |
| 273 | + (export "context.get" (func $context.get)) |
| 274 | + (export "context.set" (func $context.set)) |
| 275 | + )))) |
| 276 | + (func (export "f") (result u32) (canon lift |
| 277 | + (core func $cm "f") |
| 278 | + (post-return (func $cm "f-pr")) |
| 279 | + )) |
| 280 | + (func (export "check") (result u32) (canon lift |
| 281 | + (core func $cm "check") |
| 282 | + )) |
| 283 | +) |
| 284 | +(assert_return (invoke "f") (u32.const 7)) |
| 285 | +(assert_return (invoke "check") (u32.const 42)) |
| 286 | + |
| 287 | + |
| 288 | +(component |
| 289 | + (type $R (resource (rep i32))) |
| 290 | + (canon resource.new $R (core func $resource.new)) |
| 291 | + (canon resource.rep $R (core func $resource.rep)) |
| 292 | + (core module $CM |
| 293 | + (import "" "resource.new" (func $resource.new (param i32) (result i32))) |
| 294 | + (import "" "resource.rep" (func $resource.rep (param i32) (result i32))) |
| 295 | + |
| 296 | + (global $handle (mut i32) (i32.const 0)) |
| 297 | + (global $saved-rep (mut i32) (i32.const 0)) |
| 298 | + |
| 299 | + (func (export "f") (result i32) |
| 300 | + (global.set $handle (call $resource.new (i32.const 123))) |
| 301 | + (i32.const 5) |
| 302 | + ) |
| 303 | + (func (export "f-pr") (param i32) |
| 304 | + (global.set $saved-rep (call $resource.rep (global.get $handle))) |
| 305 | + ) |
| 306 | + (func (export "check") (result i32) |
| 307 | + (global.get $saved-rep) |
| 308 | + ) |
| 309 | + ) |
| 310 | + (core instance $cm (instantiate $CM (with "" (instance |
| 311 | + (export "resource.new" (func $resource.new)) |
| 312 | + (export "resource.rep" (func $resource.rep)) |
| 313 | + )))) |
| 314 | + (func (export "f") (result u32) (canon lift |
| 315 | + (core func $cm "f") |
| 316 | + (post-return (func $cm "f-pr")) |
| 317 | + )) |
| 318 | + (func (export "check") (result u32) (canon lift |
| 319 | + (core func $cm "check") |
| 320 | + )) |
| 321 | +) |
| 322 | +(assert_return (invoke "f") (u32.const 5)) |
| 323 | +(assert_return (invoke "check") (u32.const 123)) |
| 324 | + |
| 325 | + |
| 326 | +(component |
| 327 | + (canon backpressure.inc (core func $bp.inc)) |
| 328 | + (canon backpressure.dec (core func $bp.dec)) |
| 329 | + (core module $CM |
| 330 | + (import "" "bp.inc" (func $bp.inc)) |
| 331 | + (import "" "bp.dec" (func $bp.dec)) |
| 332 | + |
| 333 | + (func (export "f") (result i32) |
| 334 | + (i32.const 11) |
| 335 | + ) |
| 336 | + (func (export "f-pr") (param i32) |
| 337 | + (call $bp.inc) |
| 338 | + (call $bp.dec) |
| 339 | + ) |
| 340 | + ) |
| 341 | + (core instance $cm (instantiate $CM (with "" (instance |
| 342 | + (export "bp.inc" (func $bp.inc)) |
| 343 | + (export "bp.dec" (func $bp.dec)) |
| 344 | + )))) |
| 345 | + (func (export "f") (result u32) (canon lift |
| 346 | + (core func $cm "f") |
| 347 | + (post-return (func $cm "f-pr")) |
| 348 | + )) |
| 349 | +) |
| 350 | +(assert_return (invoke "f") (u32.const 11)) |
| 351 | + |
| 352 | + |
| 353 | +(component |
| 354 | + (canon thread.index (core func $thread.index)) |
| 355 | + (core module $CM |
| 356 | + (import "" "thread.index" (func $thread.index (result i32))) |
| 357 | + |
| 358 | + (global $body-idx (mut i32) (i32.const -1)) |
| 359 | + (global $pr-idx (mut i32) (i32.const -1)) |
| 360 | + |
| 361 | + (func (export "f") (result i32) |
| 362 | + (global.set $body-idx (call $thread.index)) |
| 363 | + (i32.const 13) |
| 364 | + ) |
| 365 | + (func (export "f-pr") (param i32) |
| 366 | + (global.set $pr-idx (call $thread.index)) |
| 367 | + ) |
| 368 | + (func (export "check") (result i32) |
| 369 | + (i32.eq (global.get $body-idx) (global.get $pr-idx)) |
| 370 | + ) |
| 371 | + ) |
| 372 | + (core instance $cm (instantiate $CM (with "" (instance |
| 373 | + (export "thread.index" (func $thread.index)) |
| 374 | + )))) |
| 375 | + (func (export "f") (result u32) (canon lift |
| 376 | + (core func $cm "f") |
| 377 | + (post-return (func $cm "f-pr")) |
| 378 | + )) |
| 379 | + (func (export "check") (result u32) (canon lift |
| 380 | + (core func $cm "check") |
| 381 | + )) |
| 382 | +) |
| 383 | +(assert_return (invoke "f") (u32.const 13)) |
| 384 | +(assert_return (invoke "check") (u32.const 1)) |
0 commit comments