summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/wasm/webapi/wasm_stream_instantiate_test.html
blob: f39f6504953f5e4cc9b7f6905331237e84b5cdc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<meta charset="utf-8">
<title>WebAssembly.instantiateStreaming</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script>
  promise_test(async function() {
      const response = await fetch('resources/incrementer.wasm');
      const { instance, module } = await WebAssembly.instantiateStreaming(response);
      assert_true(instance instanceof WebAssembly.Instance);
      assert_true(module instanceof WebAssembly.Module);
  }, "instantiateStreaming using resolved response");

  promise_test(async function() {
      const response = await fetch('resources/incrementer.wasm');
      const { instance } = await WebAssembly.instantiateStreaming(response);
      assert_true(instance instanceof WebAssembly.Instance);
  }, "instantiateStreaming using resolved response and check instantiate");

  promise_test(async function() {
      const result = fetch('resources/incrementer.wasm');
      const { instance } = await WebAssembly.instantiateStreaming(result);
      assert_true(instance instanceof WebAssembly.Instance);
  }, "instantiateStreaming using promise response from fetch and check instantiate");

  promise_test(async function(t) {
      const result = fetch('resources/incrementer.wrong_mime_type.wasm');
      await promise_rejects_js(t, TypeError, WebAssembly.instantiateStreaming(result));
  }, "instantiateStreaming raise error if wrong mime type");

  promise_test(async function(t) {
      const result = fetch('resources/incrementer.no_mime_type.wasm?pipe=header(Content-Type,)');
      await promise_rejects_js(t, TypeError, WebAssembly.instantiateStreaming(result));
  }, "instantiateStreaming raise error if no mime type");

  promise_test(async function(t) {
      const result = fetch('webapi/status.py?status=404');
      await promise_rejects_js(t, TypeError, WebAssembly.instantiateStreaming(result));
  }, "instantiateStreaming raise error if 404 status");

  const getWasmUrl = fileName => {
      const host_info = get_host_info();
      const url = host_info.HTTP_ORIGIN_WITH_DIFFERENT_PORT + '/wasm/webapi/';
      return url + fileName + "?pipe=header(Access-Control-Allow-Origin,*)";
  };

  promise_test(async function() {
      const result = fetch(getWasmUrl('resources/incrementer.wasm'), {"mode": "cors"} );
      const { instance } = await WebAssembly.instantiateStreaming(result);
      assert_true(instance instanceof WebAssembly.Instance);
  }, "instantiateStreaming check CORS");

  promise_test(async function(t) {
      const result = fetch(getWasmUrl('resources/incrementer.wasm'), {"mode": "no-cors"} );
      await promise_rejects_js(t, TypeError, WebAssembly.instantiateStreaming(result));
  }, "instantiateStreaming raise error if no-cors");

  promise_test(async function() {
      const v = await fetch('resources/incrementer.wasm');
      const buffer = await v.arrayBuffer();
      const response = new Response(buffer, { headers: { "Content-Type" : "application/wasm" }});
      const { instance } = await WebAssembly.instantiateStreaming(response);
      assert_true(instance instanceof WebAssembly.Instance);
  }, "instantiateStreaming receive promise with response created from ArrayBuffer");

  promise_test(async function() {
      const v = await fetch('resources/incrementer.wasm');
      const buffer = await v.arrayBuffer();
      const stream = new ReadableStream({
        start(controller) {
          (async () => {
            await Promise.resolve().then(() => controller.enqueue(new Uint8Array(buffer.slice(0, 20))));
            await Promise.resolve().then(() => controller.enqueue(new Uint8Array(buffer.slice(20, buffer.byteLength))));
            await Promise.resolve().then(() => controller.close());
          })();
        }
      });
      const response = new Response(stream, { headers: { "Content-Type" : "application/wasm" }});
      const { instance } = await WebAssembly.instantiateStreaming(response);
      assert_true(instance instanceof WebAssembly.Instance);
  }, "instantiateStreaming using ReadableStream with Uint8Array chunks");

  promise_test(async function(t) {
      const v = await fetch('resources/incrementer.wasm');
      const buffer = await v.arrayBuffer();
      const stream = new ReadableStream({
        start(controller) {
          // Enqueuing an ArrayBuffer rather a Uint8Array per
          // https://streams.spec.whatwg.org/#read-loop
          controller.enqueue(buffer);
          controller.close();
        }
      });
      const response = new Response(stream, { headers: { "Content-Type" : "application/wasm" }});
      await promise_rejects_js(t, TypeError, WebAssembly.instantiateStreaming(response));
  }, "instantiateStreaming using ReadableStream with ArrayBuffer chunk");

  promise_test(async function() {
      const response = await fetch('resources/incrementer.wasm');
      const blob = await response.blob();
      const { instance, module } = await WebAssembly.instantiateStreaming(new Response(blob, { headers: { "Content-Type" : "application/wasm" }}));
      assert_true(instance instanceof WebAssembly.Instance);
      assert_true(module instanceof WebAssembly.Module);
  }, "instantiateStreaming using blob");

  promise_test(async function(t) {
      const response = await fetch('resources/incrementer.wasm');
      const blob = await response.blob();
      const formData = new FormData;
      formData.append('blob', blob);
      formData.append('blob2', "Hello");
      await promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.instantiateStreaming(new Response(formData, { headers: { "Content-Type" : "application/wasm" }})));
  }, "instantiateStreaming using FormData");
</script>