@agoric/xsnap 0.14.3-u13.0 → 0.14.3-u16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +39 -22
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -646
@@ -0,0 +1,210 @@
1
+ /*
2
+ * Copyright (c) 2021-2022 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+ // https://url.spec.whatwg.org/#url-class
22
+
23
+ function parseURL(url) @ "fx_parseURL"
24
+ function serializeURL(url) @ "fx_serializeURL"
25
+ function parseQuery(query) @ "fx_parseQuery"
26
+ function serializeQuery(pairs) @ "fx_serializeQuery"
27
+
28
+ const SCHEME = 1;
29
+ const USERNAME = 2;
30
+ const PASSWORD = 3;
31
+ const HOST = 4;
32
+ const HOSTNAME = 5;
33
+ const PORT = 6;
34
+ const PATH = 7;
35
+ const QUERY = 8;
36
+ const FRAGMENT = 9;
37
+ const ORIGIN = 10;
38
+
39
+ class URL {
40
+ #parts;
41
+ #searchParams;
42
+ constructor(href, base) {
43
+ if (base !== undefined)
44
+ this.#parts = parseURL(href, parseURL(base));
45
+ else
46
+ this.#parts = parseURL(href);
47
+ this.#searchParams = new URLSearchParams(this.#parts.query, this.#parts);
48
+ }
49
+ get hash() {
50
+ return serializeURL(this.#parts, FRAGMENT);
51
+ }
52
+ set hash(it) {
53
+ parseURL(it, this.#parts, FRAGMENT);
54
+ }
55
+ get host() {
56
+ return serializeURL(this.#parts, HOST);
57
+ }
58
+ set host(it) {
59
+ parseURL(it, this.#parts, HOST);
60
+ }
61
+ get hostname() {
62
+ return serializeURL(this.#parts, HOSTNAME);
63
+ }
64
+ set hostname(it) {
65
+ parseURL(it, this.#parts, HOSTNAME);
66
+ }
67
+ get href() {
68
+ return serializeURL(this.#parts);
69
+ }
70
+ set href(it) {
71
+ this.#parts = parseURL(it);
72
+ this.#searchParams.updatePairs();
73
+ }
74
+ get origin() {
75
+ return serializeURL(this.#parts, ORIGIN);
76
+ }
77
+ get password() {
78
+ return serializeURL(this.#parts, PASSWORD);
79
+ }
80
+ set password(it) {
81
+ parseURL(it, this.#parts, PASSWORD);
82
+ }
83
+ get pathname() {
84
+ return serializeURL(this.#parts, PATH);
85
+ }
86
+ set pathname(it) {
87
+ parseURL(it, this.#parts, PATH);
88
+ }
89
+ get port() {
90
+ return serializeURL(this.#parts, PORT);
91
+ }
92
+ set port(it) {
93
+ parseURL(it, this.#parts, PORT);
94
+ }
95
+ get protocol() {
96
+ return serializeURL(this.#parts, SCHEME);
97
+ }
98
+ set protocol(it) {
99
+ parseURL(it, this.#parts, SCHEME);
100
+ }
101
+ get search() {
102
+ return serializeURL(this.#parts, QUERY);
103
+ }
104
+ set search(it) {
105
+ parseURL(it, this.#parts, QUERY);
106
+ this.#searchParams.updatePairs();
107
+ }
108
+ get searchParams() {
109
+ return this.#searchParams;
110
+ }
111
+ get username() {
112
+ return serializeURL(this.#parts, USERNAME);
113
+ }
114
+ set username(it) {
115
+ parseURL(it, this.#parts, USERNAME);
116
+ }
117
+ toJSON() {
118
+ return serializeURL(this.#parts);
119
+ }
120
+ toString() {
121
+ return serializeURL(this.#parts);
122
+ }
123
+ }
124
+
125
+ // https://url.spec.whatwg.org/#interface-urlsearchparams
126
+
127
+ class URLSearchParams {
128
+ #pairs = null;
129
+ #parts = null;
130
+ constructor(it = "", parts = null) {
131
+ if (typeof(it) == "object") {
132
+ this.#pairs = [];
133
+ if (Array.isArray(it)) {
134
+ for (let item of it) {
135
+ if (item.length != 2)
136
+ throw new TypeError("invalid parameter");
137
+ this.append(item[0], item[1]);
138
+ }
139
+ }
140
+ else {
141
+ for (let name in it)
142
+ this.append(name, it[name]);
143
+ }
144
+ }
145
+ else {
146
+ this.#pairs = parseQuery(it);
147
+ this.#parts = parts;
148
+ }
149
+ }
150
+ get length() {
151
+ return this.#pairs.length;
152
+ }
153
+ append(name, value) {
154
+ this.#pairs.push({ name, value });
155
+ this.updateParts();
156
+ }
157
+ delete(name) {
158
+ this.#pairs = this.#pairs.filter(pair => pair.name != name);
159
+ this.updateParts();
160
+ }
161
+ get(name) {
162
+ const pair = this.#pairs.find(pair => pair.name == name);
163
+ return pair ? pair.value : null;
164
+ }
165
+ getAll(name) {
166
+ return this.#pairs.filter(pair => pair.name == name).map(pair => pair.value);
167
+ }
168
+ has(name) {
169
+ const pair = this.#pairs.find(pair => pair.name == name);
170
+ return pair ? true : false;
171
+ }
172
+ set(name, value) {
173
+ const pair = this.#pairs.find(pair => pair.name == name);
174
+ if (pair)
175
+ pair.value = value;
176
+ else
177
+ this.#pairs.push({ name, value });
178
+ this.updateParts();
179
+ }
180
+ toString() {
181
+ return serializeQuery(this.#pairs);
182
+ }
183
+ updatePairs() {
184
+ if (this.#parts)
185
+ this.#pairs = parseQuery(this.#parts.query);
186
+ }
187
+ updateParts() {
188
+ if (this.#parts)
189
+ this.#parts.query = serializeQuery(this.#pairs);
190
+ }
191
+ [Symbol.iterator]() {
192
+ const pairs = this.#pairs
193
+ let index = 0;
194
+ const iteraror = {
195
+ next() {
196
+ if (index < pairs.length) {
197
+ const pair = pairs[index];
198
+ index++;
199
+ return { value: [pair.name, pair.value], done: false };
200
+ }
201
+ return { value: undefined, done: true };
202
+ }
203
+ }
204
+ return iteraror;
205
+ }
206
+ }
207
+
208
+ export { URL, URLSearchParams }
209
+ export default URL;
210
+
@@ -0,0 +1,8 @@
1
+ {
2
+ "modules": {
3
+ "data/wavreader": "./wavreader"
4
+ },
5
+ "preload": [
6
+ "data/wavreader"
7
+ ]
8
+ }
@@ -0,0 +1,128 @@
1
+ /*
2
+ * Copyright (c) 2016-2022 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+ class WavReader {
22
+ constructor(buffer) {
23
+ this.wav = new DataView(buffer);
24
+ this.position = 0;
25
+ this.waveSize = buffer.byteLength;
26
+
27
+ if ("RIFF" !== this.readFourCC())
28
+ throw new Error("expected RIFF");
29
+ this.seekBy(4); // file size
30
+ if ("WAVE" !== this.readFourCC())
31
+ throw new Error("expected WAVE");
32
+
33
+ if ("JUNK" === this.readFourCC())
34
+ this.seekBy(this.readUint32())
35
+ else
36
+ this.seekBy(-4);
37
+
38
+ if ("fmt " !== this.readFourCC())
39
+ throw new Error("expected fmt");
40
+ let next = this.readUint32();
41
+ next += this.position;
42
+
43
+ this.audioFormat = this.readUint16();
44
+ this.numChannels = this.readUint16();
45
+ this.sampleRate = this.readUint32();
46
+ this.seekBy(4 + 2);
47
+ this.bitsPerSample = this.readUint16();
48
+
49
+ this.seekTo(next);
50
+
51
+ while ("data" !== this.readFourCC())
52
+ this.seekBy(this.readUint32());
53
+
54
+ this.samples = Math.floor(this.readUint32() / ((this.numChannels * this.bitsPerSample) >> 3));
55
+ }
56
+
57
+ getSamples(buffer, count) { // always returns signed 16-bit sample values
58
+ this.samples -= count;
59
+ if (this.samples < 0)
60
+ throw Error("out of samples");
61
+
62
+ count *= this.numChannels;
63
+ let i = 0;
64
+ if (16 === this.bitsPerSample) {
65
+ while (count--)
66
+ buffer[i++] = this.readInt16();
67
+ }
68
+ else
69
+ if (8 == this.bitsPerSample) {
70
+ while (count--) {
71
+ let value = this.readInt8();
72
+ buffer[i++] = value << 8; // write Uint8 representation of value into low bits
73
+ }
74
+ }
75
+ }
76
+
77
+ readFourCC() {
78
+ let result = String.fromCharCode(this.wav.getUint8(this.position), this.wav.getUint8(this.position + 1),
79
+ this.wav.getUint8(this.position + 2), this.wav.getUint8(this.position + 3));
80
+ this.position += 4;
81
+ if (this.position > this.waveSize)
82
+ throw new Error("eof");
83
+ return result;
84
+ }
85
+ readUint32() {
86
+ let result = this.wav.getUint32(this.position, true);
87
+ this.position += 4;
88
+ if (this.position > this.waveSize)
89
+ throw new Error("eof");
90
+ return result;
91
+ }
92
+ readUint16() {
93
+ let result = this.wav.getUint16(this.position, true);
94
+ this.position += 2;
95
+ if (this.position > this.waveSize)
96
+ throw new Error("eof");
97
+ return result;
98
+ }
99
+ readInt16() {
100
+ let result = this.wav.getInt16(this.position, true);
101
+ this.position += 2;
102
+ if (this.position > this.waveSize)
103
+ throw new Error("eof");
104
+ return result;
105
+ }
106
+ readUint8() {
107
+ let result = this.wav.getUint8(this.position, true);
108
+ this.position += 1;
109
+ if (this.position > this.waveSize)
110
+ throw new Error("eof");
111
+ return result;
112
+ }
113
+ readInt8() {
114
+ let result = this.wav.getInt8(this.position, true);
115
+ this.position += 1;
116
+ if (this.position > this.waveSize)
117
+ throw new Error("eof");
118
+ return result;
119
+ }
120
+ seekBy(count) {
121
+ this.position += count;
122
+ }
123
+ seekTo(position) {
124
+ this.position = position;
125
+ }
126
+ }
127
+
128
+ export default WavReader;
@@ -0,0 +1,161 @@
1
+ /*
2
+ * Copyright (c) 2019-2022 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+ #include "xsmc.h"
22
+ #include "xsHost.h"
23
+ #include "mc.xs.h" // for xsID_ values
24
+
25
+ #define MINIZ_HEADER_FILE_ONLY
26
+ #include "miniz.c"
27
+
28
+ static void *zAlloc(void *opaque, size_t items, size_t size)
29
+ {
30
+ return c_malloc(items * size);
31
+ }
32
+
33
+ static void zFree(void *opaque, void *address)
34
+ {
35
+ if (address)
36
+ c_free(address);
37
+ }
38
+
39
+ void xs_deflate_destructor(void *data)
40
+ {
41
+ if (data) {
42
+ deflateEnd((z_stream *)data);
43
+ c_free(data);
44
+ }
45
+ }
46
+
47
+ void xs_deflate(xsMachine *the)
48
+ {
49
+ int windowBits = Z_DEFAULT_WINDOW_BITS, level = MZ_DEFAULT_COMPRESSION, strategy = Z_DEFAULT_STRATEGY;
50
+ z_stream *zlib;
51
+
52
+ xsmcVars(1);
53
+ if (xsmcHas(xsArg(0), xsID_windowBits)) {
54
+ xsmcGet(xsVar(0), xsArg(0), xsID_windowBits);
55
+ windowBits = xsmcToInteger(xsVar(0));
56
+ if ((15 != windowBits) && (-15 != windowBits))
57
+ xsRangeError("invalid windowBits");
58
+ }
59
+
60
+ if (xsmcHas(xsArg(0), xsID_level)) {
61
+ xsmcGet(xsVar(0), xsArg(0), xsID_level);
62
+ level = xsmcToInteger(xsVar(0));
63
+ if ((level < -1) || (level > 10))
64
+ xsRangeError("invalid level");
65
+ }
66
+
67
+ if (xsmcHas(xsArg(0), xsID_strategy)) {
68
+ xsmcGet(xsVar(0), xsArg(0), xsID_strategy);
69
+ strategy = xsmcToInteger(xsVar(0));
70
+ if ((strategy < 0) || (strategy > 4))
71
+ xsRangeError("invalid level");
72
+ }
73
+
74
+ zlib = c_malloc(sizeof(z_stream));
75
+ if (!zlib)
76
+ xsUnknownError("no memory");
77
+
78
+ zlib->zalloc = zAlloc;
79
+ zlib->zfree = zFree;
80
+
81
+ if (Z_OK != deflateInit2(zlib, level, MZ_DEFLATED, windowBits, 1, strategy)) {
82
+ c_free(zlib);
83
+ xsUnknownError("deflateInit2 failed");
84
+ }
85
+
86
+ xsmcSetHostData(xsThis, zlib);
87
+ }
88
+
89
+ void xs_deflate_close(xsMachine *the)
90
+ {
91
+ z_stream *zlib = xsmcGetHostData(xsThis);
92
+ if (zlib && xsmcGetHostDataValidate(xsThis, xs_deflate_destructor)) {
93
+ xs_deflate_destructor(zlib);
94
+ xsmcSetHostData(xsThis, NULL);
95
+ xsmcSetHostDestructor(xsThis, NULL);
96
+ }
97
+ }
98
+
99
+ void xs_deflate_push(xsMachine *the)
100
+ {
101
+ z_stream *zlib = xsmcGetHostDataValidate(xsThis, xs_deflate_destructor);
102
+ uint8_t output[1024];
103
+ int inputOffset = 0;
104
+ xsUnsignedValue inputRemaining;
105
+ void *input;
106
+ int inputEnd = (xsmcArgc > 1) ? xsmcTest(xsArg(1)) : 0;
107
+ int status = Z_OK;
108
+ uint8_t isString = xsStringType == xsmcTypeOf(xsArg(0));
109
+
110
+ if (isString)
111
+ inputRemaining = c_strlen(xsmcToString(xsArg(0)));
112
+ else
113
+ xsmcGetBufferReadable(xsArg(0), &input, &inputRemaining);
114
+
115
+ while ((0 != inputRemaining) || (Z_OK == status)) {
116
+ zlib->next_out = output;
117
+ zlib->avail_out = sizeof(output);
118
+ zlib->total_out = 0;
119
+
120
+ if (isString)
121
+ zlib->next_in = inputOffset + (uint8_t *)xsmcToString(xsArg(0));
122
+ else {
123
+ xsUnsignedValue ignore;
124
+ xsmcGetBufferReadable(xsArg(0), &input, &ignore);
125
+ zlib->next_in = inputOffset + (uint8_t *)input;
126
+ }
127
+ zlib->avail_in = inputRemaining;
128
+ zlib->total_in = 0;
129
+
130
+ status = deflate(zlib, (inputEnd ? Z_FINISH : Z_NO_FLUSH));
131
+ if (Z_BUF_ERROR == status) {
132
+ status = Z_OK;
133
+ goto bail;
134
+ }
135
+ if ((Z_OK != status) && (Z_STREAM_END != status)) {
136
+ xs_deflate_close(the);
137
+
138
+ xsmcSetInteger(xsResult, status);
139
+ xsCall1(xsThis, xsID_onEnd, xsResult);
140
+ goto bail;
141
+ }
142
+
143
+ if (zlib->total_out) {
144
+ xsmcSetArrayBuffer(xsResult, output, zlib->total_out);
145
+ xsCall1(xsThis, xsID_onData, xsResult);
146
+ }
147
+
148
+ inputOffset += zlib->total_in;
149
+ inputRemaining -= zlib->total_in;
150
+ }
151
+
152
+ if (inputEnd || (status == Z_STREAM_END)) {
153
+ xs_deflate_close(the);
154
+
155
+ xsmcSetInteger(xsResult, (Z_STREAM_END == status) ? Z_OK : status);
156
+ xsCall1(xsThis, xsID_onEnd, xsResult);
157
+ }
158
+
159
+ bail:
160
+ xsmcSetBoolean(xsResult, (Z_OK == status) || (Z_STREAM_END == status));
161
+ }
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) 2019-2022 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+
22
+ class Deflate @ "xs_deflate_destructor" {
23
+ err = 0;
24
+ chunks = [];
25
+
26
+ constructor(options = {}) {
27
+ build.call(this, options);
28
+ this.onData = onData;
29
+ this.onEnd = onEnd;
30
+ }
31
+ close() @ "xs_deflate_close";
32
+
33
+ push(buffer, end) @ "xs_deflate_push";
34
+ }
35
+
36
+ function build(options) @ "xs_deflate";
37
+
38
+ function onData(chunk) {
39
+ this.chunks.push(chunk);
40
+ }
41
+
42
+ function onEnd(err) {
43
+ this.err = err;
44
+ if (err) {
45
+ delete this.chunks;
46
+ return;
47
+ }
48
+
49
+ // join chunks
50
+ let total = 0;
51
+ for (let i = 0, chunks = this.chunks, length = chunks.length; i < length; i++)
52
+ total += chunks[i].byteLength;
53
+
54
+ this.result = new Uint8Array(total);
55
+ let offset = 0;
56
+ for (let i = 0, chunks = this.chunks, length = chunks.length; i < length; i++) {
57
+ this.result.set(new Uint8Array(this.chunks[i]), offset);
58
+ offset += chunks[i].byteLength;
59
+ }
60
+ delete this.chunks;
61
+ }
62
+
63
+ export default Deflate;
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Copyright (c) 2019-2022 Moddable Tech, Inc.
3
+ *
4
+ * This file is part of the Moddable SDK Runtime.
5
+ *
6
+ * The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published by
8
+ * the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
18
+ *
19
+ */
20
+
21
+
22
+ #include "xsmc.h"
23
+ #include "xsHost.h"
24
+ #include "mc.xs.h" // for xsID_ values
25
+
26
+ #define MINIZ_HEADER_FILE_ONLY
27
+ #include "miniz.c"
28
+
29
+ static void *zAlloc(void *opaque, size_t items, size_t size)
30
+ {
31
+ return c_malloc(items * size);
32
+ }
33
+
34
+ static void zFree(void *opaque, void *address)
35
+ {
36
+ if (address)
37
+ c_free(address);
38
+ }
39
+
40
+ void xs_inflate_destructor(void *data)
41
+ {
42
+ if (data) {
43
+ inflateEnd((z_stream *)data);
44
+ c_free(data);
45
+ }
46
+ }
47
+
48
+ void xs_inflate(xsMachine *the)
49
+ {
50
+ int windowBits = 15;
51
+ z_stream *zlib;
52
+
53
+ xsmcVars(1);
54
+ if (xsmcHas(xsArg(0), xsID_windowBits)) {
55
+ xsmcGet(xsVar(0), xsArg(0), xsID_windowBits);
56
+ windowBits = xsmcToInteger(xsVar(0));
57
+ if ((15 != windowBits) && (-15 != windowBits))
58
+ xsRangeError("invalid windowBits");
59
+ }
60
+
61
+ zlib = c_malloc(sizeof(z_stream));
62
+ if (!zlib)
63
+ xsUnknownError("no memory");
64
+
65
+ zlib->zalloc = zAlloc;
66
+ zlib->zfree = zFree;
67
+
68
+ if (Z_OK != inflateInit2(zlib, windowBits)) {
69
+ c_free(zlib);
70
+ xsUnknownError("inflateInit2 failed");
71
+ }
72
+
73
+ xsmcSetHostData(xsThis, zlib);
74
+ }
75
+
76
+ void xs_inflate_close(xsMachine *the)
77
+ {
78
+ z_stream *zlib = xsmcGetHostData(xsThis);
79
+ if (zlib && xsmcGetHostDataValidate(xsThis, xs_inflate_destructor)) {
80
+ xs_inflate_destructor(zlib);
81
+ xsmcSetHostData(xsThis, NULL);
82
+ xsmcSetHostDestructor(xsThis, NULL);
83
+ }
84
+ }
85
+
86
+ void xs_inflate_push(xsMachine *the)
87
+ {
88
+ z_stream *zlib = xsmcGetHostDataValidate(xsThis, xs_inflate_destructor);
89
+ uint8_t output[1024];
90
+ int inputOffset = 0;
91
+ uint8_t *input;
92
+ xsUnsignedValue inputRemaining, ignore;
93
+ //@@int inputEnd = xsmcTest(xsArg(1));
94
+ int inputEnd = 0;
95
+ int status = Z_OK;
96
+
97
+ xsmcGetBufferReadable(xsArg(0), (void **)&input, &inputRemaining);
98
+ while (Z_OK == status) {
99
+ zlib->next_out = output;
100
+ zlib->avail_out = sizeof(output);
101
+ zlib->total_out = 0;
102
+
103
+ xsmcGetBufferReadable(xsArg(0), (void **)&input, &ignore);
104
+ zlib->next_in = inputOffset + input;
105
+ zlib->avail_in = inputRemaining;
106
+ zlib->total_in = 0;
107
+
108
+ status = inflate(zlib, (inputEnd ? Z_FINISH : MZ_NO_FLUSH));
109
+ if ((Z_OK != status) && (Z_STREAM_END != status)) {
110
+ if (Z_DATA_ERROR == status) {
111
+ xs_inflate_close(the);
112
+ xsUnknownError("bad zlib data");
113
+ }
114
+ }
115
+
116
+ if (zlib->total_out) {
117
+ xsmcSetArrayBuffer(xsResult, output, zlib->total_out);
118
+ xsCall1(xsThis, xsID_onData, xsResult);
119
+ }
120
+
121
+ inputOffset += zlib->total_in;
122
+ inputRemaining -= zlib->total_in;
123
+
124
+ if (Z_STREAM_END == status)
125
+ break;
126
+
127
+ if (Z_BUF_ERROR == status) {
128
+ if (!inputRemaining && !zlib->total_out)
129
+ break;
130
+ status = Z_OK;
131
+ }
132
+ }
133
+
134
+ if (inputEnd || (status == Z_STREAM_END)) {
135
+ if (Z_STREAM_END != status)
136
+ inflateEnd(zlib);
137
+
138
+ xs_inflate_close(the);
139
+
140
+ xsmcSetInteger(xsResult, (Z_STREAM_END == status) ? Z_OK : status);
141
+ xsCall1(xsThis, xsID_onEnd, xsResult);
142
+ }
143
+
144
+ xsmcSetInteger(xsResult, inputRemaining);
145
+ }