Skip to content

Commit ea0eb7f

Browse files
committed
Add a simple UTF8 converter, i.e. for calling C++ APIs; Fix disabling 'abort' entirely
1 parent 73417a9 commit ea0eb7f

File tree

13 files changed

+1733
-10
lines changed

13 files changed

+1733
-10
lines changed

bin/asc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ exports.main = function main(argv, options, callback) {
357357
// Initialize default aliases
358358
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
359359
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
360-
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/env/abort");
360+
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/env/abort"); // to disable: --use abort=
361361

362362
// Add or override aliases if specified
363363
var aliases = args.use;
@@ -369,7 +369,7 @@ exports.main = function main(argv, options, callback) {
369369
if (p < 0) return callback(Error("Global alias '" + part + "' is invalid."));
370370
let name = part.substring(0, p).trim();
371371
let alias = part.substring(p + 1).trim();
372-
if (!name.length || !alias.length) return callback(Error("Global alias '" + part + "' is invalid."));
372+
if (!name.length) return callback(Error("Global alias '" + part + "' is invalid."));
373373
assemblyscript.setGlobalAlias(compilerOptions, name, alias);
374374
}
375375
}

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

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.

src/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export class Program extends DiagnosticEmitter {
416416
var globalAliases = options.globalAliases;
417417
if (globalAliases) {
418418
for (let [alias, name] of globalAliases) {
419-
if (!alias.length) continue; // explicitly disabled
419+
if (!name.length) continue; // explicitly disabled
420420
let element = this.elementsLookup.get(name);
421421
if (element) this.elementsLookup.set(alias, element);
422422
else throw new Error("element not found: " + name);

std/assembly.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ declare class String {
403403
static fromCodePoint(cp: i32): string;
404404
static fromCodePoints(arr: i32[]): string;
405405

406-
readonly length: u32;
406+
readonly length: i32;
407+
readonly lengthUTF8: i32;
407408

408409
charAt(index: u32): string;
409410
charCodeAt(index: u32): u16;
@@ -419,6 +420,7 @@ declare class String {
419420
trimRight(): string;
420421
repeat(count?: i32): string;
421422
toString(): string;
423+
toUTF8(): usize;
422424
}
423425

424426
/** Class for representing a runtime error. Base class of all errors. */

std/assembly/string.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,66 @@ export class String {
412412
toString(): String {
413413
return this;
414414
}
415+
416+
get lengthUTF8(): i32 {
417+
var clen = this.length;
418+
var blen = 1; // null terminated
419+
for (let i = 0; i < clen;) {
420+
let c = <u32>load<u16>(changetype<usize>(this) + (<usize>i << 1), HEADER_SIZE);
421+
if (c < 128) {
422+
blen += 1; ++i;
423+
} else if (c < 2048) {
424+
blen += 2; ++i;
425+
} else if (
426+
(c & 0xFC00) === 0xD800 &&
427+
(<u32>load<u16>(changetype<usize>(this) + ((<usize>i + 1) << 1), HEADER_SIZE) & 0xFC00) === 0xDC00
428+
) {
429+
blen += 4; i += 2;
430+
} else {
431+
blen += 3; ++i;
432+
}
433+
}
434+
return blen;
435+
}
436+
437+
toUTF8(): usize {
438+
var len = this.lengthUTF8;
439+
var buf = allocate_memory(len);
440+
var off: usize = 0;
441+
for (let i = 0, k = this.length; i < k;) {
442+
let c1 = <u32>load<u16>(changetype<usize>(this) + (<usize>i << 1), HEADER_SIZE);
443+
if (c1 < 128) {
444+
store<u8>(buf + off, c1);
445+
++off; ++i;
446+
} else if (c1 < 2048) {
447+
let pos = buf + off;
448+
store<u8>(pos, c1 >> 6 | 192, 0);
449+
store<u8>(pos, c1 & 63 | 128, 1);
450+
off += 2; ++i;
451+
} else {
452+
let pos = buf + off;
453+
if ((c1 & 0xFC00) == 0xD800) {
454+
let c2 = <u32>load<u16>(changetype<usize>(this) + ((<usize>i + 1) << 1), HEADER_SIZE);
455+
if ((c2 & 0xFC00) == 0xDC00) {
456+
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
457+
store<u8>(pos, c1 >> 18 | 240, 0);
458+
store<u8>(pos, c1 >> 12 & 63 | 128, 1);
459+
store<u8>(pos, c1 >> 6 & 63 | 128, 2);
460+
store<u8>(pos, c1 & 63 | 128, 3);
461+
off += 4; i += 2;
462+
continue;
463+
}
464+
}
465+
store<u8>(pos, c1 >> 12 | 224, 0);
466+
store<u8>(pos, c1 >> 6 & 63 | 128, 1);
467+
store<u8>(pos, c1 & 63 | 128, 2);
468+
off += 3; ++i;
469+
}
470+
}
471+
assert(off == len - 1);
472+
store<u8>(buf + off, 0);
473+
return buf;
474+
}
415475
}
416476

417477
export function parseInt(str: String, radix: i32 = 0): f64 {

0 commit comments

Comments
 (0)