Skip to content

Commit 2e5077d

Browse files
committed
Remove unnecessary dependencies in asc bundle; Update dependencies
1 parent 60f75c9 commit 2e5077d

File tree

8 files changed

+303
-311
lines changed

8 files changed

+303
-311
lines changed

bin/asc.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
const fs = require("fs");
1515
const path = require("path");
16-
const os = require("os");
16+
const utf8 = require("./util/utf8");
17+
const EOL = process.platform === "win32" ? "\r\n" : "\n";
1718

1819
// Use distribution files if present, otherwise run the sources directly
1920
var assemblyscript, isDev;
@@ -152,15 +153,15 @@ exports.main = function main(argv, options, callback) {
152153
if (!callback) callback = function defaultCallback(err) {
153154
var code = 0;
154155
if (err) {
155-
stderr.write(err.stack + os.EOL);
156+
stderr.write(err.stack + EOL);
156157
code = 1;
157158
}
158159
return code;
159160
};
160161

161162
// Just print the version if requested
162163
if (args.version) {
163-
stdout.write("Version " + exports.version + (isDev ? "-dev" : "") + os.EOL);
164+
stdout.write("Version " + exports.version + (isDev ? "-dev" : "") + EOL);
164165
return callback(null);
165166
}
166167
// Print the help message if requested or no source files are provided
@@ -181,7 +182,7 @@ exports.main = function main(argv, options, callback) {
181182
for (let i = 0; i < indent; ++i) {
182183
line = " " + line;
183184
}
184-
return os.EOL + line;
185+
return EOL + line;
185186
}).join(""));
186187
} else {
187188
opts.push(text + option.desc);
@@ -197,7 +198,7 @@ exports.main = function main(argv, options, callback) {
197198
" asc hello1.ts hello2.ts -b -O > hello.wasm",
198199
"",
199200
"Options:"
200-
].concat(opts).join(os.EOL) + os.EOL);
201+
].concat(opts).join(EOL) + EOL);
201202
return callback(null);
202203
}
203204

@@ -566,7 +567,7 @@ exports.main = function main(argv, options, callback) {
566567
path.basename(sourceMapURL)
567568
), JSON.stringify(sourceMap));
568569
} else {
569-
stderr.write("Skipped source map (stdout already occupied)" + os.EOL);
570+
stderr.write("Skipped source map (stdout already occupied)" + EOL);
570571
}
571572
}
572573
}
@@ -741,7 +742,7 @@ function checkDiagnostics(emitter, stderr) {
741742
while ((diagnostic = assemblyscript.nextDiagnostic(emitter)) != null) {
742743
stderr.write(
743744
assemblyscript.formatDiagnostic(diagnostic, stderr.isTTY, true) +
744-
os.EOL + os.EOL
745+
EOL + EOL
745746
);
746747
if (assemblyscript.isError(diagnostic)) hasErrors = true;
747748
}
@@ -803,24 +804,36 @@ function printStats(stats, output) {
803804
"Emit : " + format(stats.emitTime, stats.emitCount),
804805
"Validate : " + format(stats.validateTime, stats.validateCount),
805806
"Optimize : " + format(stats.optimizeTime, stats.optimizeCount)
806-
].join(os.EOL) + os.EOL);
807+
].join(EOL) + EOL);
807808
}
808809

809810
exports.printStats = printStats;
810811

812+
var Buf = typeof global !== "undefined" && global.Buffer || Uint8Array;
813+
811814
/** Creates a memory stream that can be used in place of stdout/stderr. */
812815
function createMemoryStream(fn) {
813816
var stream = [];
814817
stream.write = function(chunk) {
815818
if (typeof chunk === "string") {
816-
this.push(Buffer.from(chunk, "utf8"));
817-
} else {
818-
this.push(chunk);
819+
let buffer = new Buf(utf8.length(chunk));
820+
utf8.write(chunk, buffer, 0);
821+
chunk = buffer;
819822
}
823+
this.push(chunk);
820824
if (fn) fn(chunk);
821825
};
822826
stream.toBuffer = function() {
823-
return Buffer.concat(this);
827+
var offset = 0, i = 0, k = this.length;
828+
while (i < k) offset += this[i++].length;
829+
var buffer = new Buf(offset);
830+
offset = i = 0;
831+
while (i < k) {
832+
buffer.set(this[i], offset);
833+
offset += this[i].length;
834+
++i;
835+
}
836+
return buffer;
824837
};
825838
stream.toString = function() {
826839
return this.toBuffer().toString("utf8");

bin/util/utf8.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// see: https://github.com/dcodeIO/protobuf.js/tree/master/lib/utf8
2+
3+
exports.length = function utf8_length(string) {
4+
var len = 0,
5+
c = 0;
6+
for (var i = 0; i < string.length; ++i) {
7+
c = string.charCodeAt(i);
8+
if (c < 128) {
9+
len += 1;
10+
} else if (c < 2048) {
11+
len += 2;
12+
} else if ((c & 0xFC00) === 0xD800 && (string.charCodeAt(i + 1) & 0xFC00) === 0xDC00) {
13+
++i;
14+
len += 4;
15+
} else {
16+
len += 3;
17+
}
18+
}
19+
return len;
20+
};
21+
22+
exports.write = function utf8_write(string, buffer, offset) {
23+
var start = offset,
24+
c1, c2;
25+
for (var i = 0; i < string.length; ++i) {
26+
c1 = string.charCodeAt(i);
27+
if (c1 < 128) {
28+
buffer[offset++] = c1;
29+
} else if (c1 < 2048) {
30+
buffer[offset++] = c1 >> 6 | 192;
31+
buffer[offset++] = c1 & 63 | 128;
32+
} else if ((c1 & 0xFC00) === 0xD800 && ((c2 = string.charCodeAt(i + 1)) & 0xFC00) === 0xDC00) {
33+
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
34+
++i;
35+
buffer[offset++] = c1 >> 18 | 240;
36+
buffer[offset++] = c1 >> 12 & 63 | 128;
37+
buffer[offset++] = c1 >> 6 & 63 | 128;
38+
buffer[offset++] = c1 & 63 | 128;
39+
} else {
40+
buffer[offset++] = c1 >> 12 | 224;
41+
buffer[offset++] = c1 >> 6 & 63 | 128;
42+
buffer[offset++] = c1 & 63 | 128;
43+
}
44+
}
45+
return offset - start;
46+
};

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)