├── .cargo-ok ├── .gitignore ├── .prettierrc ├── README.md ├── assembly ├── index.ts └── tsconfig.json ├── build ├── .gitignore └── optimized.wat ├── package-lock.json ├── package.json ├── src ├── handler.ts ├── index.ts └── types.d.ts ├── test ├── _setup.ts ├── handler.ts └── tsconfig.json ├── tsconfig.json ├── webpack.config.js └── wrangler.toml /.cargo-ok: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JamesLMilner/worker-assemblyscript-template/363431b8cdadd97cfaff81113530b237e5cb9fc3/.cargo-ok -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | transpiled 4 | worker 5 | build -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": false, 4 | "trailingComma": "all", 5 | "tabWidth": 2, 6 | "printWidth": 80 7 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # worker-assemblyscript-template 2 | 3 | A batteries included template for kick starting a [AssemblyScript](https://docs.assemblyscript.org/) Cloudflare worker project. The template is based on the TypeScript template, so uses TypeScript for the request handler. 4 | 5 | AssemblyScript compiles a strict subset of TypeScript (a typed superset of JavaScript) to WebAssembly using Binaryen. 6 | 7 | ## 🔋 Getting Started 8 | 9 | This template is meant to be used with [`wrangler`](https://github.com/cloudflare/wrangler). If you are not already familiar with the tool, we recommend that you install the tool and configure it to work with your [Cloudflare account](https://dash.cloudflare.com). 10 | 11 | To generate using `wrangler`, run this command: 12 | 13 | ```bash 14 | wrangler generate my-asc-project https://github.com/JamesLMilner/worker-assemblyscript-template 15 | ``` 16 | 17 | ### 👩 💻 Developing 18 | 19 | [`src/index.js`](https://github.com/JamesLMilner/worker-assemblyscript-template/blob/master/src/index.ts) calls the request handler in [`src/handler.ts`](https://github.com/JamesLMilner/worker-assemblyscript-template/blob/master/src/handler.ts), and will return the [request method](https://developer.mozilla.org/en-US/docs/Web/API/Request/method) for the given request. 20 | 21 | For your AssemblyScript code you can edit `assembly/index.ts` and call `npm run asbuild` to see your changes. To build the whole worker you can do `npm run build`, which will build your TypeScript and AssemblyScript. 22 | 23 | ### 🧪 Testing 24 | 25 | This template comes with mocha tests which simply test that the request handler can handle each request method. `npm test` will run your tests. 26 | 27 | ### ✏️ Formatting 28 | 29 | This template uses [`prettier`](https://prettier.io/) to format the project. To invoke, run `npm run format`. 30 | 31 | ### 👀 Previewing and Publishing 32 | 33 | For information on how to preview and publish your worker, please see the `wrangler` [README](https://github.com/cloudflare/wrangler#%EF%B8%8F--publish). 34 | 35 | ## 🤢 Issues 36 | 37 | If you run into issues with this specific project, please feel free to file an issue [here](https://github.com/JamesLMilner/worker-assemblyscript-template/issues). If the problem is with `wrangler`, please file an issue [here](https://github.com/cloudflare/wrangler/issues). 38 | 39 | ## ⚠️ Caveats 40 | 41 | The `service-worker-mock` used by the tests is not a perfect representation of the Cloudflare Workers runtime. It is a general approximation. We recommend that you test with `wrangler preview` and in a staging environment to test things before deploying. 42 | 43 | ## Acknowledgements 44 | 45 | This template is in turn based off of the Cloudflare TypeScript template available [here](https://github.com/EverlastingBugstopper/worker-typescript-template). -------------------------------------------------------------------------------- /assembly/index.ts: -------------------------------------------------------------------------------- 1 | // The entry file of your WebAssembly module. 2 | 3 | export function add(a: i32, b: i32): i32 { 4 | return a + b; 5 | } 6 | -------------------------------------------------------------------------------- /assembly/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../node_modules/assemblyscript/std/assembly.json", 3 | "include": [ 4 | "./**/*.ts" 5 | ] 6 | } -------------------------------------------------------------------------------- /build/.gitignore: -------------------------------------------------------------------------------- 1 | *.wasm 2 | *.wasm.map 3 | *.asm.js 4 | -------------------------------------------------------------------------------- /build/optimized.wat: -------------------------------------------------------------------------------- 1 | (module 2 | (type $FUNCSIG$iii (func (param i32 i32) (result i32))) 3 | (type $FUNCSIG$v (func)) 4 | (type $FUNCSIG$viiii (func (param i32 i32 i32 i32))) 5 | (type $FUNCSIG$vii (func (param i32 i32))) 6 | (type $FUNCSIG$ii (func (param i32) (result i32))) 7 | (type $FUNCSIG$viii (func (param i32 i32 i32))) 8 | (type $FUNCSIG$vi (func (param i32))) 9 | (import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32))) 10 | (memory $0 1) 11 | (data (i32.const 8) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00t\00l\00s\00f\00.\00t\00s") 12 | (data (i32.const 56) "(\00\00\00\01\00\00\00\01\00\00\00(\00\00\00a\00l\00l\00o\00c\00a\00t\00i\00o\00n\00 \00t\00o\00o\00 \00l\00a\00r\00g\00e") 13 | (data (i32.const 112) "\1e\00\00\00\01\00\00\00\01\00\00\00\1e\00\00\00~\00l\00i\00b\00/\00r\00t\00/\00p\00u\00r\00e\00.\00t\00s") 14 | (data (i32.const 160) "$\00\00\00\01\00\00\00\01\00\00\00$\00\00\00I\00n\00d\00e\00x\00 \00o\00u\00t\00 \00o\00f\00 \00r\00a\00n\00g\00e") 15 | (data (i32.const 216) "\14\00\00\00\01\00\00\00\01\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s") 16 | (data (i32.const 256) "\03\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\10") 17 | (global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0)) 18 | (global $~lib/rt/pure/CUR (mut i32) (i32.const 0)) 19 | (global $~lib/rt/pure/END (mut i32) (i32.const 0)) 20 | (global $~lib/rt/pure/ROOTS (mut i32) (i32.const 0)) 21 | (global $~lib/rt/__rtti_base i32 (i32.const 256)) 22 | (export "memory" (memory $0)) 23 | (export "__alloc" (func $~lib/rt/tlsf/__alloc)) 24 | (export "__retain" (func $~lib/rt/pure/__retain)) 25 | (export "__release" (func $~lib/rt/pure/__release)) 26 | (export "__collect" (func $~lib/rt/pure/__collect)) 27 | (export "__rtti_base" (global $~lib/rt/__rtti_base)) 28 | (export "add" (func $assembly/index/add)) 29 | (func $~lib/rt/tlsf/removeBlock (; 1 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 30 | (local $2 i32) 31 | (local $3 i32) 32 | (local $4 i32) 33 | (local $5 i32) 34 | local.get $1 35 | i32.load 36 | local.tee $3 37 | i32.const 1 38 | i32.and 39 | i32.eqz 40 | if 41 | i32.const 0 42 | i32.const 24 43 | i32.const 277 44 | i32.const 13 45 | call $~lib/builtins/abort 46 | unreachable 47 | end 48 | local.get $3 49 | i32.const -4 50 | i32.and 51 | local.tee $2 52 | i32.const 16 53 | i32.ge_u 54 | if (result i32) 55 | local.get $2 56 | i32.const 1073741808 57 | i32.lt_u 58 | else 59 | i32.const 0 60 | end 61 | i32.eqz 62 | if 63 | i32.const 0 64 | i32.const 24 65 | i32.const 279 66 | i32.const 13 67 | call $~lib/builtins/abort 68 | unreachable 69 | end 70 | local.get $2 71 | i32.const 256 72 | i32.lt_u 73 | if (result i32) 74 | local.get $2 75 | i32.const 4 76 | i32.shr_u 77 | local.set $2 78 | i32.const 0 79 | else 80 | local.get $2 81 | i32.const 31 82 | local.get $2 83 | i32.clz 84 | i32.sub 85 | local.tee $3 86 | i32.const 4 87 | i32.sub 88 | i32.shr_u 89 | i32.const 16 90 | i32.xor 91 | local.set $2 92 | local.get $3 93 | i32.const 7 94 | i32.sub 95 | end 96 | local.tee $3 97 | i32.const 23 98 | i32.lt_u 99 | if (result i32) 100 | local.get $2 101 | i32.const 16 102 | i32.lt_u 103 | else 104 | i32.const 0 105 | end 106 | i32.eqz 107 | if 108 | i32.const 0 109 | i32.const 24 110 | i32.const 292 111 | i32.const 13 112 | call $~lib/builtins/abort 113 | unreachable 114 | end 115 | local.get $1 116 | i32.load offset=20 117 | local.set $4 118 | local.get $1 119 | i32.load offset=16 120 | local.tee $5 121 | if 122 | local.get $5 123 | local.get $4 124 | i32.store offset=20 125 | end 126 | local.get $4 127 | if 128 | local.get $4 129 | local.get $5 130 | i32.store offset=16 131 | end 132 | local.get $3 133 | i32.const 4 134 | i32.shl 135 | local.get $2 136 | i32.add 137 | i32.const 2 138 | i32.shl 139 | local.get $0 140 | i32.add 141 | i32.load offset=96 142 | local.get $1 143 | i32.eq 144 | if 145 | local.get $3 146 | i32.const 4 147 | i32.shl 148 | local.get $2 149 | i32.add 150 | i32.const 2 151 | i32.shl 152 | local.get $0 153 | i32.add 154 | local.get $4 155 | i32.store offset=96 156 | local.get $4 157 | i32.eqz 158 | if 159 | local.get $3 160 | i32.const 2 161 | i32.shl 162 | local.get $0 163 | i32.add 164 | local.get $3 165 | i32.const 2 166 | i32.shl 167 | local.get $0 168 | i32.add 169 | i32.load offset=4 170 | i32.const 1 171 | local.get $2 172 | i32.shl 173 | i32.const -1 174 | i32.xor 175 | i32.and 176 | local.tee $1 177 | i32.store offset=4 178 | local.get $1 179 | i32.eqz 180 | if 181 | local.get $0 182 | local.get $0 183 | i32.load 184 | i32.const 1 185 | local.get $3 186 | i32.shl 187 | i32.const -1 188 | i32.xor 189 | i32.and 190 | i32.store 191 | end 192 | end 193 | end 194 | ) 195 | (func $~lib/rt/tlsf/insertBlock (; 2 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 196 | (local $2 i32) 197 | (local $3 i32) 198 | (local $4 i32) 199 | (local $5 i32) 200 | (local $6 i32) 201 | (local $7 i32) 202 | local.get $1 203 | i32.eqz 204 | if 205 | i32.const 0 206 | i32.const 24 207 | i32.const 205 208 | i32.const 13 209 | call $~lib/builtins/abort 210 | unreachable 211 | end 212 | local.get $1 213 | i32.load 214 | local.tee $3 215 | i32.const 1 216 | i32.and 217 | i32.eqz 218 | if 219 | i32.const 0 220 | i32.const 24 221 | i32.const 207 222 | i32.const 13 223 | call $~lib/builtins/abort 224 | unreachable 225 | end 226 | local.get $1 227 | i32.const 16 228 | i32.add 229 | local.get $1 230 | i32.load 231 | i32.const -4 232 | i32.and 233 | i32.add 234 | local.tee $4 235 | i32.load 236 | local.tee $5 237 | i32.const 1 238 | i32.and 239 | if 240 | local.get $3 241 | i32.const -4 242 | i32.and 243 | i32.const 16 244 | i32.add 245 | local.get $5 246 | i32.const -4 247 | i32.and 248 | i32.add 249 | local.tee $2 250 | i32.const 1073741808 251 | i32.lt_u 252 | if 253 | local.get $0 254 | local.get $4 255 | call $~lib/rt/tlsf/removeBlock 256 | local.get $1 257 | local.get $3 258 | i32.const 3 259 | i32.and 260 | local.get $2 261 | i32.or 262 | local.tee $3 263 | i32.store 264 | local.get $1 265 | i32.const 16 266 | i32.add 267 | local.get $1 268 | i32.load 269 | i32.const -4 270 | i32.and 271 | i32.add 272 | local.tee $4 273 | i32.load 274 | local.set $5 275 | end 276 | end 277 | local.get $3 278 | i32.const 2 279 | i32.and 280 | if 281 | local.get $1 282 | i32.const 4 283 | i32.sub 284 | i32.load 285 | local.tee $2 286 | i32.load 287 | local.tee $6 288 | i32.const 1 289 | i32.and 290 | i32.eqz 291 | if 292 | i32.const 0 293 | i32.const 24 294 | i32.const 228 295 | i32.const 15 296 | call $~lib/builtins/abort 297 | unreachable 298 | end 299 | local.get $6 300 | i32.const -4 301 | i32.and 302 | i32.const 16 303 | i32.add 304 | local.get $3 305 | i32.const -4 306 | i32.and 307 | i32.add 308 | local.tee $7 309 | i32.const 1073741808 310 | i32.lt_u 311 | if (result i32) 312 | local.get $0 313 | local.get $2 314 | call $~lib/rt/tlsf/removeBlock 315 | local.get $2 316 | local.get $6 317 | i32.const 3 318 | i32.and 319 | local.get $7 320 | i32.or 321 | local.tee $3 322 | i32.store 323 | local.get $2 324 | else 325 | local.get $1 326 | end 327 | local.set $1 328 | end 329 | local.get $4 330 | local.get $5 331 | i32.const 2 332 | i32.or 333 | i32.store 334 | local.get $3 335 | i32.const -4 336 | i32.and 337 | local.tee $2 338 | i32.const 16 339 | i32.ge_u 340 | if (result i32) 341 | local.get $2 342 | i32.const 1073741808 343 | i32.lt_u 344 | else 345 | i32.const 0 346 | end 347 | i32.eqz 348 | if 349 | i32.const 0 350 | i32.const 24 351 | i32.const 243 352 | i32.const 13 353 | call $~lib/builtins/abort 354 | unreachable 355 | end 356 | local.get $4 357 | local.get $1 358 | i32.const 16 359 | i32.add 360 | local.get $2 361 | i32.add 362 | i32.ne 363 | if 364 | i32.const 0 365 | i32.const 24 366 | i32.const 244 367 | i32.const 13 368 | call $~lib/builtins/abort 369 | unreachable 370 | end 371 | local.get $4 372 | i32.const 4 373 | i32.sub 374 | local.get $1 375 | i32.store 376 | local.get $2 377 | i32.const 256 378 | i32.lt_u 379 | if (result i32) 380 | local.get $2 381 | i32.const 4 382 | i32.shr_u 383 | local.set $4 384 | i32.const 0 385 | else 386 | local.get $2 387 | i32.const 31 388 | local.get $2 389 | i32.clz 390 | i32.sub 391 | local.tee $2 392 | i32.const 4 393 | i32.sub 394 | i32.shr_u 395 | i32.const 16 396 | i32.xor 397 | local.set $4 398 | local.get $2 399 | i32.const 7 400 | i32.sub 401 | end 402 | local.tee $3 403 | i32.const 23 404 | i32.lt_u 405 | if (result i32) 406 | local.get $4 407 | i32.const 16 408 | i32.lt_u 409 | else 410 | i32.const 0 411 | end 412 | i32.eqz 413 | if 414 | i32.const 0 415 | i32.const 24 416 | i32.const 260 417 | i32.const 13 418 | call $~lib/builtins/abort 419 | unreachable 420 | end 421 | local.get $3 422 | i32.const 4 423 | i32.shl 424 | local.get $4 425 | i32.add 426 | i32.const 2 427 | i32.shl 428 | local.get $0 429 | i32.add 430 | i32.load offset=96 431 | local.set $2 432 | local.get $1 433 | i32.const 0 434 | i32.store offset=16 435 | local.get $1 436 | local.get $2 437 | i32.store offset=20 438 | local.get $2 439 | if 440 | local.get $2 441 | local.get $1 442 | i32.store offset=16 443 | end 444 | local.get $3 445 | i32.const 4 446 | i32.shl 447 | local.get $4 448 | i32.add 449 | i32.const 2 450 | i32.shl 451 | local.get $0 452 | i32.add 453 | local.get $1 454 | i32.store offset=96 455 | local.get $0 456 | local.get $0 457 | i32.load 458 | i32.const 1 459 | local.get $3 460 | i32.shl 461 | i32.or 462 | i32.store 463 | local.get $3 464 | i32.const 2 465 | i32.shl 466 | local.get $0 467 | i32.add 468 | local.get $3 469 | i32.const 2 470 | i32.shl 471 | local.get $0 472 | i32.add 473 | i32.load offset=4 474 | i32.const 1 475 | local.get $4 476 | i32.shl 477 | i32.or 478 | i32.store offset=4 479 | ) 480 | (func $~lib/rt/tlsf/addMemory (; 3 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32) 481 | (local $3 i32) 482 | (local $4 i32) 483 | local.get $2 484 | i32.const 15 485 | i32.and 486 | i32.eqz 487 | i32.const 0 488 | local.get $1 489 | i32.const 15 490 | i32.and 491 | i32.eqz 492 | i32.const 0 493 | local.get $1 494 | local.get $2 495 | i32.le_u 496 | select 497 | select 498 | i32.eqz 499 | if 500 | i32.const 0 501 | i32.const 24 502 | i32.const 386 503 | i32.const 4 504 | call $~lib/builtins/abort 505 | unreachable 506 | end 507 | local.get $0 508 | i32.load offset=1568 509 | local.tee $3 510 | if 511 | local.get $1 512 | local.get $3 513 | i32.const 16 514 | i32.add 515 | i32.lt_u 516 | if 517 | i32.const 0 518 | i32.const 24 519 | i32.const 396 520 | i32.const 15 521 | call $~lib/builtins/abort 522 | unreachable 523 | end 524 | local.get $1 525 | i32.const 16 526 | i32.sub 527 | local.get $3 528 | i32.eq 529 | if 530 | local.get $3 531 | i32.load 532 | local.set $4 533 | local.get $1 534 | i32.const 16 535 | i32.sub 536 | local.set $1 537 | end 538 | else 539 | local.get $1 540 | local.get $0 541 | i32.const 1572 542 | i32.add 543 | i32.lt_u 544 | if 545 | i32.const 0 546 | i32.const 24 547 | i32.const 408 548 | i32.const 4 549 | call $~lib/builtins/abort 550 | unreachable 551 | end 552 | end 553 | local.get $2 554 | local.get $1 555 | i32.sub 556 | local.tee $2 557 | i32.const 48 558 | i32.lt_u 559 | if 560 | return 561 | end 562 | local.get $1 563 | local.get $4 564 | i32.const 2 565 | i32.and 566 | local.get $2 567 | i32.const 32 568 | i32.sub 569 | i32.const 1 570 | i32.or 571 | i32.or 572 | i32.store 573 | local.get $1 574 | i32.const 0 575 | i32.store offset=16 576 | local.get $1 577 | i32.const 0 578 | i32.store offset=20 579 | local.get $1 580 | local.get $2 581 | i32.add 582 | i32.const 16 583 | i32.sub 584 | local.tee $2 585 | i32.const 2 586 | i32.store 587 | local.get $0 588 | local.get $2 589 | i32.store offset=1568 590 | local.get $0 591 | local.get $1 592 | call $~lib/rt/tlsf/insertBlock 593 | ) 594 | (func $~lib/rt/tlsf/initializeRoot (; 4 ;) (type $FUNCSIG$v) 595 | (local $0 i32) 596 | (local $1 i32) 597 | i32.const 1 598 | memory.size 599 | local.tee $0 600 | i32.gt_s 601 | if (result i32) 602 | i32.const 1 603 | local.get $0 604 | i32.sub 605 | memory.grow 606 | i32.const 0 607 | i32.lt_s 608 | else 609 | i32.const 0 610 | end 611 | if 612 | unreachable 613 | end 614 | i32.const 288 615 | i32.const 0 616 | i32.store 617 | i32.const 1856 618 | i32.const 0 619 | i32.store 620 | i32.const 0 621 | local.set $0 622 | loop $loop|0 623 | block $break|0 624 | local.get $0 625 | i32.const 23 626 | i32.ge_u 627 | br_if $break|0 628 | local.get $0 629 | i32.const 2 630 | i32.shl 631 | i32.const 288 632 | i32.add 633 | i32.const 0 634 | i32.store offset=4 635 | i32.const 0 636 | local.set $1 637 | loop $loop|1 638 | block $break|1 639 | local.get $1 640 | i32.const 16 641 | i32.ge_u 642 | br_if $break|1 643 | local.get $0 644 | i32.const 4 645 | i32.shl 646 | local.get $1 647 | i32.add 648 | i32.const 2 649 | i32.shl 650 | i32.const 288 651 | i32.add 652 | i32.const 0 653 | i32.store offset=96 654 | local.get $1 655 | i32.const 1 656 | i32.add 657 | local.set $1 658 | br $loop|1 659 | end 660 | end 661 | local.get $0 662 | i32.const 1 663 | i32.add 664 | local.set $0 665 | br $loop|0 666 | end 667 | end 668 | i32.const 288 669 | i32.const 1872 670 | memory.size 671 | i32.const 16 672 | i32.shl 673 | call $~lib/rt/tlsf/addMemory 674 | i32.const 288 675 | global.set $~lib/rt/tlsf/ROOT 676 | ) 677 | (func $~lib/rt/tlsf/prepareSize (; 5 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32) 678 | local.get $0 679 | i32.const 1073741808 680 | i32.ge_u 681 | if 682 | i32.const 72 683 | i32.const 24 684 | i32.const 457 685 | i32.const 29 686 | call $~lib/builtins/abort 687 | unreachable 688 | end 689 | local.get $0 690 | i32.const 15 691 | i32.add 692 | i32.const -16 693 | i32.and 694 | local.tee $0 695 | i32.const 16 696 | local.get $0 697 | i32.const 16 698 | i32.gt_u 699 | select 700 | ) 701 | (func $~lib/rt/tlsf/searchBlock (; 6 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) 702 | (local $2 i32) 703 | local.get $1 704 | i32.const 256 705 | i32.lt_u 706 | if (result i32) 707 | local.get $1 708 | i32.const 4 709 | i32.shr_u 710 | local.set $1 711 | i32.const 0 712 | else 713 | local.get $1 714 | i32.const 536870904 715 | i32.lt_u 716 | if 717 | i32.const 1 718 | i32.const 27 719 | local.get $1 720 | i32.clz 721 | i32.sub 722 | i32.shl 723 | local.get $1 724 | i32.add 725 | i32.const 1 726 | i32.sub 727 | local.set $1 728 | end 729 | local.get $1 730 | i32.const 31 731 | local.get $1 732 | i32.clz 733 | i32.sub 734 | local.tee $2 735 | i32.const 4 736 | i32.sub 737 | i32.shr_u 738 | i32.const 16 739 | i32.xor 740 | local.set $1 741 | local.get $2 742 | i32.const 7 743 | i32.sub 744 | end 745 | local.tee $2 746 | i32.const 23 747 | i32.lt_u 748 | if (result i32) 749 | local.get $1 750 | i32.const 16 751 | i32.lt_u 752 | else 753 | i32.const 0 754 | end 755 | i32.eqz 756 | if 757 | i32.const 0 758 | i32.const 24 759 | i32.const 338 760 | i32.const 13 761 | call $~lib/builtins/abort 762 | unreachable 763 | end 764 | local.get $2 765 | i32.const 2 766 | i32.shl 767 | local.get $0 768 | i32.add 769 | i32.load offset=4 770 | i32.const -1 771 | local.get $1 772 | i32.shl 773 | i32.and 774 | local.tee $1 775 | if (result i32) 776 | local.get $1 777 | i32.ctz 778 | local.get $2 779 | i32.const 4 780 | i32.shl 781 | i32.add 782 | i32.const 2 783 | i32.shl 784 | local.get $0 785 | i32.add 786 | i32.load offset=96 787 | else 788 | local.get $0 789 | i32.load 790 | i32.const -1 791 | local.get $2 792 | i32.const 1 793 | i32.add 794 | i32.shl 795 | i32.and 796 | local.tee $1 797 | if (result i32) 798 | local.get $1 799 | i32.ctz 800 | local.tee $1 801 | i32.const 2 802 | i32.shl 803 | local.get $0 804 | i32.add 805 | i32.load offset=4 806 | local.tee $2 807 | i32.eqz 808 | if 809 | i32.const 0 810 | i32.const 24 811 | i32.const 351 812 | i32.const 17 813 | call $~lib/builtins/abort 814 | unreachable 815 | end 816 | local.get $2 817 | i32.ctz 818 | local.get $1 819 | i32.const 4 820 | i32.shl 821 | i32.add 822 | i32.const 2 823 | i32.shl 824 | local.get $0 825 | i32.add 826 | i32.load offset=96 827 | else 828 | i32.const 0 829 | end 830 | end 831 | ) 832 | (func $~lib/rt/tlsf/growMemory (; 7 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 833 | (local $2 i32) 834 | memory.size 835 | local.tee $2 836 | i32.const 16 837 | local.get $0 838 | i32.load offset=1568 839 | local.get $2 840 | i32.const 16 841 | i32.shl 842 | i32.const 16 843 | i32.sub 844 | i32.ne 845 | i32.shl 846 | i32.const 1 847 | i32.const 27 848 | local.get $1 849 | i32.clz 850 | i32.sub 851 | i32.shl 852 | i32.const 1 853 | i32.sub 854 | local.get $1 855 | i32.add 856 | local.get $1 857 | local.get $1 858 | i32.const 536870904 859 | i32.lt_u 860 | select 861 | i32.add 862 | i32.const 65535 863 | i32.add 864 | i32.const -65536 865 | i32.and 866 | i32.const 16 867 | i32.shr_u 868 | local.tee $1 869 | local.get $2 870 | local.get $1 871 | i32.gt_s 872 | select 873 | memory.grow 874 | i32.const 0 875 | i32.lt_s 876 | if 877 | local.get $1 878 | memory.grow 879 | i32.const 0 880 | i32.lt_s 881 | if 882 | unreachable 883 | end 884 | end 885 | local.get $0 886 | local.get $2 887 | i32.const 16 888 | i32.shl 889 | memory.size 890 | i32.const 16 891 | i32.shl 892 | call $~lib/rt/tlsf/addMemory 893 | ) 894 | (func $~lib/rt/tlsf/prepareBlock (; 8 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32) 895 | (local $3 i32) 896 | (local $4 i32) 897 | local.get $1 898 | i32.load 899 | local.set $3 900 | local.get $2 901 | i32.const 15 902 | i32.and 903 | if 904 | i32.const 0 905 | i32.const 24 906 | i32.const 365 907 | i32.const 13 908 | call $~lib/builtins/abort 909 | unreachable 910 | end 911 | local.get $3 912 | i32.const -4 913 | i32.and 914 | local.get $2 915 | i32.sub 916 | local.tee $4 917 | i32.const 32 918 | i32.ge_u 919 | if 920 | local.get $1 921 | local.get $3 922 | i32.const 2 923 | i32.and 924 | local.get $2 925 | i32.or 926 | i32.store 927 | local.get $1 928 | i32.const 16 929 | i32.add 930 | local.get $2 931 | i32.add 932 | local.tee $1 933 | local.get $4 934 | i32.const 16 935 | i32.sub 936 | i32.const 1 937 | i32.or 938 | i32.store 939 | local.get $0 940 | local.get $1 941 | call $~lib/rt/tlsf/insertBlock 942 | else 943 | local.get $1 944 | local.get $3 945 | i32.const -2 946 | i32.and 947 | i32.store 948 | local.get $1 949 | i32.const 16 950 | i32.add 951 | local.get $1 952 | i32.load 953 | i32.const -4 954 | i32.and 955 | i32.add 956 | local.get $1 957 | i32.const 16 958 | i32.add 959 | local.get $1 960 | i32.load 961 | i32.const -4 962 | i32.and 963 | i32.add 964 | i32.load 965 | i32.const -3 966 | i32.and 967 | i32.store 968 | end 969 | ) 970 | (func $~lib/rt/tlsf/allocateBlock (; 9 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) 971 | (local $2 i32) 972 | (local $3 i32) 973 | local.get $0 974 | local.get $1 975 | call $~lib/rt/tlsf/prepareSize 976 | local.tee $3 977 | call $~lib/rt/tlsf/searchBlock 978 | local.tee $2 979 | i32.eqz 980 | if 981 | local.get $0 982 | local.get $3 983 | call $~lib/rt/tlsf/growMemory 984 | local.get $0 985 | local.get $3 986 | call $~lib/rt/tlsf/searchBlock 987 | local.tee $2 988 | i32.eqz 989 | if 990 | i32.const 0 991 | i32.const 24 992 | i32.const 487 993 | i32.const 15 994 | call $~lib/builtins/abort 995 | unreachable 996 | end 997 | end 998 | local.get $2 999 | i32.load 1000 | i32.const -4 1001 | i32.and 1002 | local.get $3 1003 | i32.lt_u 1004 | if 1005 | i32.const 0 1006 | i32.const 24 1007 | i32.const 489 1008 | i32.const 13 1009 | call $~lib/builtins/abort 1010 | unreachable 1011 | end 1012 | local.get $2 1013 | i32.const 0 1014 | i32.store offset=4 1015 | local.get $2 1016 | local.get $1 1017 | i32.store offset=12 1018 | local.get $0 1019 | local.get $2 1020 | call $~lib/rt/tlsf/removeBlock 1021 | local.get $0 1022 | local.get $2 1023 | local.get $3 1024 | call $~lib/rt/tlsf/prepareBlock 1025 | local.get $2 1026 | ) 1027 | (func $~lib/rt/tlsf/__alloc (; 10 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) 1028 | (local $2 i32) 1029 | global.get $~lib/rt/tlsf/ROOT 1030 | local.tee $2 1031 | if (result i32) 1032 | local.get $2 1033 | else 1034 | call $~lib/rt/tlsf/initializeRoot 1035 | global.get $~lib/rt/tlsf/ROOT 1036 | end 1037 | local.get $0 1038 | call $~lib/rt/tlsf/allocateBlock 1039 | local.tee $0 1040 | local.get $1 1041 | i32.store offset=8 1042 | local.get $0 1043 | i32.const 16 1044 | i32.add 1045 | ) 1046 | (func $~lib/rt/pure/increment (; 11 ;) (type $FUNCSIG$vi) (param $0 i32) 1047 | (local $1 i32) 1048 | local.get $0 1049 | i32.load offset=4 1050 | local.tee $1 1051 | i32.const -268435456 1052 | i32.and 1053 | local.get $1 1054 | i32.const 1 1055 | i32.add 1056 | i32.const -268435456 1057 | i32.and 1058 | i32.ne 1059 | if 1060 | i32.const 0 1061 | i32.const 128 1062 | i32.const 104 1063 | i32.const 2 1064 | call $~lib/builtins/abort 1065 | unreachable 1066 | end 1067 | local.get $0 1068 | local.get $1 1069 | i32.const 1 1070 | i32.add 1071 | i32.store offset=4 1072 | local.get $0 1073 | i32.load 1074 | i32.const 1 1075 | i32.and 1076 | if 1077 | i32.const 0 1078 | i32.const 128 1079 | i32.const 107 1080 | i32.const 13 1081 | call $~lib/builtins/abort 1082 | unreachable 1083 | end 1084 | ) 1085 | (func $~lib/rt/pure/__retain (; 12 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32) 1086 | local.get $0 1087 | i32.const 284 1088 | i32.gt_u 1089 | if 1090 | local.get $0 1091 | i32.const 16 1092 | i32.sub 1093 | call $~lib/rt/pure/increment 1094 | end 1095 | local.get $0 1096 | ) 1097 | (func $~lib/rt/tlsf/freeBlock (; 13 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 1098 | (local $2 i32) 1099 | local.get $1 1100 | i32.load 1101 | local.tee $2 1102 | i32.const 1 1103 | i32.and 1104 | if 1105 | i32.const 0 1106 | i32.const 24 1107 | i32.const 546 1108 | i32.const 2 1109 | call $~lib/builtins/abort 1110 | unreachable 1111 | end 1112 | local.get $1 1113 | local.get $2 1114 | i32.const 1 1115 | i32.or 1116 | i32.store 1117 | local.get $0 1118 | local.get $1 1119 | call $~lib/rt/tlsf/insertBlock 1120 | ) 1121 | (func $~lib/rt/__typeinfo (; 14 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32) 1122 | local.get $0 1123 | i32.const 256 1124 | i32.load 1125 | i32.gt_u 1126 | if 1127 | i32.const 176 1128 | i32.const 232 1129 | i32.const 22 1130 | i32.const 27 1131 | call $~lib/builtins/abort 1132 | unreachable 1133 | end 1134 | local.get $0 1135 | i32.const 3 1136 | i32.shl 1137 | i32.const 260 1138 | i32.add 1139 | i32.load 1140 | ) 1141 | (func $~lib/memory/memory.copy (; 15 ;) (type $FUNCSIG$viii) (param $0 i32) (param $1 i32) (param $2 i32) 1142 | (local $3 i32) 1143 | (local $4 i32) 1144 | block $~lib/util/memory/memmove|inlined.0 1145 | local.get $2 1146 | local.set $3 1147 | local.get $0 1148 | local.get $1 1149 | i32.eq 1150 | br_if $~lib/util/memory/memmove|inlined.0 1151 | local.get $0 1152 | local.get $1 1153 | i32.lt_u 1154 | if 1155 | local.get $1 1156 | i32.const 7 1157 | i32.and 1158 | local.get $0 1159 | i32.const 7 1160 | i32.and 1161 | i32.eq 1162 | if 1163 | loop $continue|0 1164 | local.get $0 1165 | i32.const 7 1166 | i32.and 1167 | if 1168 | local.get $3 1169 | i32.eqz 1170 | br_if $~lib/util/memory/memmove|inlined.0 1171 | local.get $3 1172 | i32.const 1 1173 | i32.sub 1174 | local.set $3 1175 | local.get $0 1176 | local.tee $2 1177 | i32.const 1 1178 | i32.add 1179 | local.set $0 1180 | local.get $1 1181 | local.tee $4 1182 | i32.const 1 1183 | i32.add 1184 | local.set $1 1185 | local.get $2 1186 | local.get $4 1187 | i32.load8_u 1188 | i32.store8 1189 | br $continue|0 1190 | end 1191 | end 1192 | loop $continue|1 1193 | local.get $3 1194 | i32.const 8 1195 | i32.lt_u 1196 | i32.eqz 1197 | if 1198 | local.get $0 1199 | local.get $1 1200 | i64.load 1201 | i64.store 1202 | local.get $3 1203 | i32.const 8 1204 | i32.sub 1205 | local.set $3 1206 | local.get $0 1207 | i32.const 8 1208 | i32.add 1209 | local.set $0 1210 | local.get $1 1211 | i32.const 8 1212 | i32.add 1213 | local.set $1 1214 | br $continue|1 1215 | end 1216 | end 1217 | end 1218 | loop $continue|2 1219 | local.get $3 1220 | if 1221 | local.get $0 1222 | local.tee $2 1223 | i32.const 1 1224 | i32.add 1225 | local.set $0 1226 | local.get $1 1227 | local.tee $4 1228 | i32.const 1 1229 | i32.add 1230 | local.set $1 1231 | local.get $2 1232 | local.get $4 1233 | i32.load8_u 1234 | i32.store8 1235 | local.get $3 1236 | i32.const 1 1237 | i32.sub 1238 | local.set $3 1239 | br $continue|2 1240 | end 1241 | end 1242 | else 1243 | local.get $1 1244 | i32.const 7 1245 | i32.and 1246 | local.get $0 1247 | i32.const 7 1248 | i32.and 1249 | i32.eq 1250 | if 1251 | loop $continue|3 1252 | local.get $0 1253 | local.get $3 1254 | i32.add 1255 | i32.const 7 1256 | i32.and 1257 | if 1258 | local.get $3 1259 | i32.eqz 1260 | br_if $~lib/util/memory/memmove|inlined.0 1261 | local.get $0 1262 | local.get $3 1263 | i32.const 1 1264 | i32.sub 1265 | local.tee $3 1266 | i32.add 1267 | local.get $1 1268 | local.get $3 1269 | i32.add 1270 | i32.load8_u 1271 | i32.store8 1272 | br $continue|3 1273 | end 1274 | end 1275 | loop $continue|4 1276 | local.get $3 1277 | i32.const 8 1278 | i32.lt_u 1279 | i32.eqz 1280 | if 1281 | local.get $0 1282 | local.get $3 1283 | i32.const 8 1284 | i32.sub 1285 | local.tee $3 1286 | i32.add 1287 | local.get $1 1288 | local.get $3 1289 | i32.add 1290 | i64.load 1291 | i64.store 1292 | br $continue|4 1293 | end 1294 | end 1295 | end 1296 | loop $continue|5 1297 | local.get $3 1298 | if 1299 | local.get $0 1300 | local.get $3 1301 | i32.const 1 1302 | i32.sub 1303 | local.tee $3 1304 | i32.add 1305 | local.get $1 1306 | local.get $3 1307 | i32.add 1308 | i32.load8_u 1309 | i32.store8 1310 | br $continue|5 1311 | end 1312 | end 1313 | end 1314 | end 1315 | ) 1316 | (func $~lib/rt/tlsf/__free (; 16 ;) (type $FUNCSIG$vi) (param $0 i32) 1317 | global.get $~lib/rt/tlsf/ROOT 1318 | i32.eqz 1319 | if 1320 | i32.const 0 1321 | i32.const 24 1322 | i32.const 576 1323 | i32.const 13 1324 | call $~lib/builtins/abort 1325 | unreachable 1326 | end 1327 | local.get $0 1328 | i32.const 15 1329 | i32.and 1330 | i32.eqz 1331 | i32.const 0 1332 | local.get $0 1333 | select 1334 | i32.eqz 1335 | if 1336 | i32.const 0 1337 | i32.const 24 1338 | i32.const 577 1339 | i32.const 2 1340 | call $~lib/builtins/abort 1341 | unreachable 1342 | end 1343 | global.get $~lib/rt/tlsf/ROOT 1344 | local.get $0 1345 | i32.const 16 1346 | i32.sub 1347 | call $~lib/rt/tlsf/freeBlock 1348 | ) 1349 | (func $~lib/rt/pure/growRoots (; 17 ;) (type $FUNCSIG$v) 1350 | (local $0 i32) 1351 | (local $1 i32) 1352 | (local $2 i32) 1353 | (local $3 i32) 1354 | global.get $~lib/rt/pure/CUR 1355 | global.get $~lib/rt/pure/ROOTS 1356 | local.tee $1 1357 | i32.sub 1358 | local.tee $2 1359 | i32.const 1 1360 | i32.shl 1361 | local.tee $0 1362 | i32.const 256 1363 | local.get $0 1364 | i32.const 256 1365 | i32.gt_u 1366 | select 1367 | local.tee $3 1368 | i32.const 0 1369 | call $~lib/rt/tlsf/__alloc 1370 | local.tee $0 1371 | local.get $1 1372 | local.get $2 1373 | call $~lib/memory/memory.copy 1374 | local.get $1 1375 | if 1376 | local.get $1 1377 | call $~lib/rt/tlsf/__free 1378 | end 1379 | local.get $0 1380 | global.set $~lib/rt/pure/ROOTS 1381 | local.get $0 1382 | local.get $2 1383 | i32.add 1384 | global.set $~lib/rt/pure/CUR 1385 | local.get $0 1386 | local.get $3 1387 | i32.add 1388 | global.set $~lib/rt/pure/END 1389 | ) 1390 | (func $~lib/rt/pure/appendRoot (; 18 ;) (type $FUNCSIG$vi) (param $0 i32) 1391 | (local $1 i32) 1392 | global.get $~lib/rt/pure/CUR 1393 | local.tee $1 1394 | global.get $~lib/rt/pure/END 1395 | i32.ge_u 1396 | if 1397 | call $~lib/rt/pure/growRoots 1398 | global.get $~lib/rt/pure/CUR 1399 | local.set $1 1400 | end 1401 | local.get $1 1402 | local.get $0 1403 | i32.store 1404 | local.get $1 1405 | i32.const 4 1406 | i32.add 1407 | global.set $~lib/rt/pure/CUR 1408 | ) 1409 | (func $~lib/rt/pure/decrement (; 19 ;) (type $FUNCSIG$vi) (param $0 i32) 1410 | (local $1 i32) 1411 | (local $2 i32) 1412 | local.get $0 1413 | i32.load offset=4 1414 | local.tee $2 1415 | i32.const 268435455 1416 | i32.and 1417 | local.set $1 1418 | local.get $0 1419 | i32.load 1420 | i32.const 1 1421 | i32.and 1422 | if 1423 | i32.const 0 1424 | i32.const 128 1425 | i32.const 115 1426 | i32.const 13 1427 | call $~lib/builtins/abort 1428 | unreachable 1429 | end 1430 | local.get $1 1431 | i32.const 1 1432 | i32.eq 1433 | if 1434 | local.get $0 1435 | i32.const 16 1436 | i32.add 1437 | i32.const 1 1438 | call $~lib/rt/__visit_members 1439 | local.get $2 1440 | i32.const -2147483648 1441 | i32.and 1442 | if 1443 | local.get $0 1444 | i32.const -2147483648 1445 | i32.store offset=4 1446 | else 1447 | global.get $~lib/rt/tlsf/ROOT 1448 | local.get $0 1449 | call $~lib/rt/tlsf/freeBlock 1450 | end 1451 | else 1452 | local.get $1 1453 | i32.const 0 1454 | i32.le_u 1455 | if 1456 | i32.const 0 1457 | i32.const 128 1458 | i32.const 124 1459 | i32.const 15 1460 | call $~lib/builtins/abort 1461 | unreachable 1462 | end 1463 | local.get $0 1464 | i32.load offset=8 1465 | call $~lib/rt/__typeinfo 1466 | i32.const 16 1467 | i32.and 1468 | if 1469 | local.get $0 1470 | local.get $1 1471 | i32.const 1 1472 | i32.sub 1473 | local.get $2 1474 | i32.const -268435456 1475 | i32.and 1476 | i32.or 1477 | i32.store offset=4 1478 | else 1479 | local.get $0 1480 | local.get $1 1481 | i32.const 1 1482 | i32.sub 1483 | i32.const -1342177280 1484 | i32.or 1485 | i32.store offset=4 1486 | local.get $2 1487 | i32.const -2147483648 1488 | i32.and 1489 | i32.eqz 1490 | if 1491 | local.get $0 1492 | call $~lib/rt/pure/appendRoot 1493 | end 1494 | end 1495 | end 1496 | ) 1497 | (func $~lib/rt/pure/__release (; 20 ;) (type $FUNCSIG$vi) (param $0 i32) 1498 | local.get $0 1499 | i32.const 284 1500 | i32.gt_u 1501 | if 1502 | local.get $0 1503 | i32.const 16 1504 | i32.sub 1505 | call $~lib/rt/pure/decrement 1506 | end 1507 | ) 1508 | (func $~lib/rt/pure/markGray (; 21 ;) (type $FUNCSIG$vi) (param $0 i32) 1509 | (local $1 i32) 1510 | local.get $0 1511 | i32.load offset=4 1512 | local.tee $1 1513 | i32.const 1879048192 1514 | i32.and 1515 | i32.const 268435456 1516 | i32.ne 1517 | if 1518 | local.get $0 1519 | local.get $1 1520 | i32.const -1879048193 1521 | i32.and 1522 | i32.const 268435456 1523 | i32.or 1524 | i32.store offset=4 1525 | local.get $0 1526 | i32.const 16 1527 | i32.add 1528 | i32.const 2 1529 | call $~lib/rt/__visit_members 1530 | end 1531 | ) 1532 | (func $~lib/rt/pure/scanBlack (; 22 ;) (type $FUNCSIG$vi) (param $0 i32) 1533 | local.get $0 1534 | local.get $0 1535 | i32.load offset=4 1536 | i32.const -1879048193 1537 | i32.and 1538 | i32.store offset=4 1539 | local.get $0 1540 | i32.const 16 1541 | i32.add 1542 | i32.const 4 1543 | call $~lib/rt/__visit_members 1544 | ) 1545 | (func $~lib/rt/pure/scan (; 23 ;) (type $FUNCSIG$vi) (param $0 i32) 1546 | (local $1 i32) 1547 | local.get $0 1548 | i32.load offset=4 1549 | local.tee $1 1550 | i32.const 1879048192 1551 | i32.and 1552 | i32.const 268435456 1553 | i32.eq 1554 | if 1555 | local.get $1 1556 | i32.const 268435455 1557 | i32.and 1558 | i32.const 0 1559 | i32.gt_u 1560 | if 1561 | local.get $0 1562 | call $~lib/rt/pure/scanBlack 1563 | else 1564 | local.get $0 1565 | local.get $1 1566 | i32.const -1879048193 1567 | i32.and 1568 | i32.const 536870912 1569 | i32.or 1570 | i32.store offset=4 1571 | local.get $0 1572 | i32.const 16 1573 | i32.add 1574 | i32.const 3 1575 | call $~lib/rt/__visit_members 1576 | end 1577 | end 1578 | ) 1579 | (func $~lib/rt/pure/collectWhite (; 24 ;) (type $FUNCSIG$vi) (param $0 i32) 1580 | (local $1 i32) 1581 | local.get $0 1582 | i32.load offset=4 1583 | local.tee $1 1584 | i32.const 1879048192 1585 | i32.and 1586 | i32.const 536870912 1587 | i32.eq 1588 | if (result i32) 1589 | local.get $1 1590 | i32.const -2147483648 1591 | i32.and 1592 | i32.eqz 1593 | else 1594 | i32.const 0 1595 | end 1596 | if 1597 | local.get $0 1598 | local.get $1 1599 | i32.const -1879048193 1600 | i32.and 1601 | i32.store offset=4 1602 | local.get $0 1603 | i32.const 16 1604 | i32.add 1605 | i32.const 5 1606 | call $~lib/rt/__visit_members 1607 | global.get $~lib/rt/tlsf/ROOT 1608 | local.get $0 1609 | call $~lib/rt/tlsf/freeBlock 1610 | end 1611 | ) 1612 | (func $~lib/rt/pure/__collect (; 25 ;) (type $FUNCSIG$v) 1613 | (local $0 i32) 1614 | (local $1 i32) 1615 | (local $2 i32) 1616 | (local $3 i32) 1617 | (local $4 i32) 1618 | (local $5 i32) 1619 | global.get $~lib/rt/pure/ROOTS 1620 | local.tee $5 1621 | local.tee $2 1622 | local.set $3 1623 | global.get $~lib/rt/pure/CUR 1624 | local.set $0 1625 | loop $loop|0 1626 | block $break|0 1627 | local.get $3 1628 | local.get $0 1629 | i32.ge_u 1630 | br_if $break|0 1631 | local.get $3 1632 | i32.load 1633 | local.tee $4 1634 | i32.load offset=4 1635 | local.tee $1 1636 | i32.const 1879048192 1637 | i32.and 1638 | i32.const 805306368 1639 | i32.eq 1640 | if (result i32) 1641 | local.get $1 1642 | i32.const 268435455 1643 | i32.and 1644 | i32.const 0 1645 | i32.gt_u 1646 | else 1647 | i32.const 0 1648 | end 1649 | if 1650 | local.get $4 1651 | call $~lib/rt/pure/markGray 1652 | local.get $2 1653 | local.get $4 1654 | i32.store 1655 | local.get $2 1656 | i32.const 4 1657 | i32.add 1658 | local.set $2 1659 | else 1660 | i32.const 0 1661 | local.get $1 1662 | i32.const 268435455 1663 | i32.and 1664 | i32.eqz 1665 | local.get $1 1666 | i32.const 1879048192 1667 | i32.and 1668 | select 1669 | if 1670 | global.get $~lib/rt/tlsf/ROOT 1671 | local.get $4 1672 | call $~lib/rt/tlsf/freeBlock 1673 | else 1674 | local.get $4 1675 | local.get $1 1676 | i32.const 2147483647 1677 | i32.and 1678 | i32.store offset=4 1679 | end 1680 | end 1681 | local.get $3 1682 | i32.const 4 1683 | i32.add 1684 | local.set $3 1685 | br $loop|0 1686 | end 1687 | end 1688 | local.get $2 1689 | global.set $~lib/rt/pure/CUR 1690 | local.get $5 1691 | local.set $0 1692 | loop $loop|1 1693 | block $break|1 1694 | local.get $0 1695 | local.get $2 1696 | i32.ge_u 1697 | br_if $break|1 1698 | local.get $0 1699 | i32.load 1700 | call $~lib/rt/pure/scan 1701 | local.get $0 1702 | i32.const 4 1703 | i32.add 1704 | local.set $0 1705 | br $loop|1 1706 | end 1707 | end 1708 | local.get $5 1709 | local.set $0 1710 | loop $loop|2 1711 | block $break|2 1712 | local.get $0 1713 | local.get $2 1714 | i32.ge_u 1715 | br_if $break|2 1716 | local.get $0 1717 | i32.load 1718 | local.tee $1 1719 | local.get $1 1720 | i32.load offset=4 1721 | i32.const 2147483647 1722 | i32.and 1723 | i32.store offset=4 1724 | local.get $1 1725 | call $~lib/rt/pure/collectWhite 1726 | local.get $0 1727 | i32.const 4 1728 | i32.add 1729 | local.set $0 1730 | br $loop|2 1731 | end 1732 | end 1733 | local.get $5 1734 | global.set $~lib/rt/pure/CUR 1735 | ) 1736 | (func $assembly/index/add (; 26 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32) 1737 | local.get $0 1738 | local.get $1 1739 | i32.add 1740 | ) 1741 | (func $~lib/rt/pure/__visit (; 27 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 1742 | local.get $0 1743 | i32.const 284 1744 | i32.lt_u 1745 | if 1746 | return 1747 | end 1748 | local.get $0 1749 | i32.const 16 1750 | i32.sub 1751 | local.set $0 1752 | block $break|0 1753 | block $case5|0 1754 | block $case4|0 1755 | block $case3|0 1756 | block $case2|0 1757 | block $case1|0 1758 | local.get $1 1759 | i32.const 1 1760 | i32.ne 1761 | if 1762 | local.get $1 1763 | i32.const 2 1764 | i32.eq 1765 | br_if $case1|0 1766 | block $tablify|0 1767 | local.get $1 1768 | i32.const 3 1769 | i32.sub 1770 | br_table $case2|0 $case3|0 $case4|0 $tablify|0 1771 | end 1772 | br $case5|0 1773 | end 1774 | local.get $0 1775 | call $~lib/rt/pure/decrement 1776 | br $break|0 1777 | end 1778 | local.get $0 1779 | i32.load offset=4 1780 | i32.const 268435455 1781 | i32.and 1782 | i32.const 0 1783 | i32.le_u 1784 | if 1785 | i32.const 0 1786 | i32.const 128 1787 | i32.const 75 1788 | i32.const 17 1789 | call $~lib/builtins/abort 1790 | unreachable 1791 | end 1792 | local.get $0 1793 | local.get $0 1794 | i32.load offset=4 1795 | i32.const 1 1796 | i32.sub 1797 | i32.store offset=4 1798 | local.get $0 1799 | call $~lib/rt/pure/markGray 1800 | br $break|0 1801 | end 1802 | local.get $0 1803 | call $~lib/rt/pure/scan 1804 | br $break|0 1805 | end 1806 | local.get $0 1807 | i32.load offset=4 1808 | local.tee $1 1809 | i32.const -268435456 1810 | i32.and 1811 | local.get $1 1812 | i32.const 1 1813 | i32.add 1814 | i32.const -268435456 1815 | i32.and 1816 | i32.ne 1817 | if 1818 | i32.const 0 1819 | i32.const 128 1820 | i32.const 86 1821 | i32.const 6 1822 | call $~lib/builtins/abort 1823 | unreachable 1824 | end 1825 | local.get $0 1826 | local.get $1 1827 | i32.const 1 1828 | i32.add 1829 | i32.store offset=4 1830 | local.get $1 1831 | i32.const 1879048192 1832 | i32.and 1833 | if 1834 | local.get $0 1835 | call $~lib/rt/pure/scanBlack 1836 | end 1837 | br $break|0 1838 | end 1839 | local.get $0 1840 | call $~lib/rt/pure/collectWhite 1841 | br $break|0 1842 | end 1843 | i32.const 0 1844 | i32.const 128 1845 | i32.const 97 1846 | i32.const 24 1847 | call $~lib/builtins/abort 1848 | unreachable 1849 | end 1850 | ) 1851 | (func $~lib/rt/__visit_members (; 28 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32) 1852 | block $switch$1$default 1853 | block $switch$1$case$4 1854 | block $switch$1$case$2 1855 | local.get $0 1856 | i32.const 8 1857 | i32.sub 1858 | i32.load 1859 | br_table $switch$1$case$2 $switch$1$case$2 $switch$1$case$4 $switch$1$default 1860 | end 1861 | return 1862 | end 1863 | local.get $0 1864 | i32.load 1865 | local.tee $0 1866 | if 1867 | local.get $0 1868 | local.get $1 1869 | call $~lib/rt/pure/__visit 1870 | end 1871 | return 1872 | end 1873 | unreachable 1874 | ) 1875 | (func $null (; 29 ;) (type $FUNCSIG$v) 1876 | nop 1877 | ) 1878 | ) 1879 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "worker-assemblyscript-template", 3 | "version": "0.0.1", 4 | "description": "Cloudflare worker AssemblyScript template", 5 | "main": "index.js", 6 | "scripts": { 7 | "asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug", 8 | "asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize", 9 | "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized", 10 | "build": "npm run asbuild:optimized && shx cp build/optimized.wasm worker/module.wasm && webpack", 11 | "dev": "NODE_ENV=development npm run build", 12 | "format": "prettier --write '**/*.{ts,js,css,json,md}'", 13 | "test:clean": "rm -rf ./transpiled/src ./transpiled/test", 14 | "test": "npm run test:clean && npm run transpile && mocha --require source-map-support/register --recursive transpiled/test", 15 | "transpile": "tsc --project ./test" 16 | }, 17 | "author": "James ", 18 | "license": "MIT", 19 | "devDependencies": { 20 | "@cloudflare/workers-types": "^1.0.1", 21 | "@types/chai": "^4.1.7", 22 | "@types/mocha": "^5.2.7", 23 | "chai": "^4.2.0", 24 | "file-loader": "^4.2.0", 25 | "mocha": "^6.1.4", 26 | "prettier": "^1.18.2", 27 | "service-worker-mock": "^2.0.3", 28 | "source-map-loader": "^0.2.4", 29 | "source-map-support": "^0.5.12", 30 | "ts-loader": "^6.0.4", 31 | "typescript": "^3.5.3", 32 | "webpack": "^4.35.3", 33 | "webpack-cli": "^3.3.6", 34 | "copy-webpack-plugin": "^5.0.4", 35 | "sinon": "^7.4.2" 36 | }, 37 | "dependencies": { 38 | "assemblyscript": "git@github.com:AssemblyScript/assemblyscript.git", 39 | "shx": "^0.3.2" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/handler.ts: -------------------------------------------------------------------------------- 1 | import { AdditionPayload } from "./types"; 2 | const loader = require("assemblyscript/lib/loader"); 3 | const wasmmodule = loader.instantiate(wasmprogram); 4 | 5 | export async function handleRequest(request: Request): Promise { 6 | 7 | if (request.method === "POST") { 8 | return await addNumbers(await request.json()); 9 | } 10 | 11 | return new Response(`request method: ${request.method}`) 12 | } 13 | 14 | async function addNumbers({ numberOne, numberTwo } : AdditionPayload) { 15 | 16 | if (typeof numberOne !== "number") { 17 | return new Response("numberOne is not a number", {status: 400 }) 18 | } 19 | 20 | if (typeof numberTwo !== "number") { 21 | return new Response("numberTwo is not a number", {status: 400 }) 22 | } 23 | 24 | const result: number = wasmmodule.add(numberOne, numberTwo) 25 | const returnPayload = JSON.stringify({result}); 26 | 27 | return new Response(returnPayload, { 28 | headers: { 29 | 'content-type': 'application/json;charset=UTF-8', 30 | }, 31 | }); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { handleRequest } from "./handler" 2 | 3 | addEventListener('fetch', event => { 4 | event.respondWith(handleRequest(event.request)) 5 | }) 6 | -------------------------------------------------------------------------------- /src/types.d.ts: -------------------------------------------------------------------------------- 1 | import { KVNamespace } from '@cloudflare/workers-types' 2 | 3 | declare global { 4 | const wasmprogram: { 5 | add: (numberOne: number, numberTwo: number) => number; 6 | }; 7 | const myKVNamespace: KVNamespace 8 | } 9 | 10 | interface AdditionPayload { 11 | numberOne: number, 12 | numberTwo: number 13 | } -------------------------------------------------------------------------------- /test/_setup.ts: -------------------------------------------------------------------------------- 1 | // set up global namespace for worker environment 2 | //@ts-ignore 3 | import * as makeServiceWorkerEnv from 'service-worker-mock' 4 | import { readFileSync } from 'fs' 5 | declare var global: any 6 | Object.assign(global, makeServiceWorkerEnv()) 7 | global.wasmprogram = new WebAssembly.Module(readFileSync(__dirname + '/../../worker/module.wasm')); -------------------------------------------------------------------------------- /test/handler.ts: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai' 2 | import { handleRequest } from '../src/handler' 3 | 4 | describe('handler returns response with request method for none POST requests', () => { 5 | const methods = [ 6 | 'GET', 7 | 'HEAD', 8 | 'PUT', 9 | 'DELETE', 10 | 'CONNECT', 11 | 'OPTIONS', 12 | 'TRACE', 13 | 'PATCH', 14 | ] 15 | methods.forEach(method => { 16 | it(method, async () => { 17 | const result = await handleRequest(new Request('/', { method })) 18 | const text = await result.text() 19 | expect(text).to.include(method) 20 | }) 21 | }) 22 | }) 23 | 24 | describe('returns two numbers for POST requests', () => { 25 | it('POST', async () => { 26 | const result = await handleRequest(new Request('/', { 27 | method: "POST", 28 | body: JSON.stringify({ numberOne: 2, numberTwo: 2 }) 29 | })) 30 | 31 | expect(result.status).to.equal(200); 32 | const json = await result.json(); 33 | expect(json).to.include({ result: 4 }); 34 | }) 35 | }) -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "../transpiled", 4 | "target": "esnext", 5 | "module": "commonjs", 6 | "sourceMap": true, 7 | "experimentalDecorators": true, 8 | "lib": ["esnext", "webworker"] 9 | }, 10 | "include": ["./*.ts", "../node_modules/@cloudflare/workers-types/index.d.ts"] 11 | } 12 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "./dist", 4 | "module": "esNext", 5 | "target": "esnext", 6 | "lib": ["esnext", "webworker"], 7 | "alwaysStrict": true, 8 | "strict": true, 9 | "preserveConstEnums": true, 10 | "moduleResolution": "node", 11 | "sourceMap": true, 12 | "esModuleInterop": true 13 | }, 14 | "include": [ 15 | "./src/*.ts", 16 | "./test/*.ts", 17 | "./src/**/*.ts", 18 | "./test/**/*.ts", 19 | "./node_modules/@cloudflare/workers-types/index.d.ts" 20 | ], 21 | "exclude": ["node_modules/", "dist/"] 22 | } 23 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const webpack = require('webpack') 3 | const CopyPlugin = require('copy-webpack-plugin') 4 | const mode = process.env.NODE_ENV || 'production' 5 | 6 | module.exports = { 7 | output: { 8 | filename: `worker.${mode}.js`, 9 | path: path.join(__dirname, 'dist'), 10 | }, 11 | devtool: 'source-map', 12 | mode, 13 | resolve: { 14 | extensions: ['.ts', '.tsx', '.js'], 15 | plugins: [], 16 | }, 17 | module: { 18 | rules: [ 19 | { 20 | test: /\.wasm$/, 21 | type: "javascript/auto", // ← !! 22 | loader: "file-loader", 23 | options: { 24 | publicPath: "dist/" 25 | } 26 | }, 27 | { 28 | test: /\.tsx?$/, 29 | loader: 'ts-loader', 30 | options: { 31 | transpileOnly: true, 32 | }, 33 | }, 34 | { enforce: 'pre', test: /\.js$/, loader: 'source-map-loader' }, 35 | ] 36 | }, 37 | } 38 | -------------------------------------------------------------------------------- /wrangler.toml: -------------------------------------------------------------------------------- 1 | name = "assemblyscript" 2 | type = "webpack" 3 | webpack_config = "webpack.config.js" 4 | zone_id = "" 5 | private = false 6 | account_id = "ae10dd550e0c312335c5be4e90775fd0" 7 | route = "" 8 | workers_dev = true --------------------------------------------------------------------------------