Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support typed array in parser #1796

Merged
merged 32 commits into from
Jan 27, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
06e8954
typed array support for parser
jerch Nov 23, 2018
14d6960
Merge remote-tracking branch 'upstream/master' into typedarray_parser
jerch Nov 23, 2018
591dfd5
fix DCS handler
jerch Nov 29, 2018
a4e3646
Merge branch 'master' into typedarray_parser
jerch Nov 29, 2018
a6ef64d
always clear DCS buffer on HOOK
jerch Nov 29, 2018
5d3ef75
Merge branch 'master' into typedarray_parser
jerch Nov 29, 2018
2483a98
use faster string conversion
jerch Nov 29, 2018
ef171bf
Merge branch 'master' into typedarray_parser
jerch Dec 15, 2018
d5bffb5
Merge branch 'master' into typedarray_parser
Tyriar Dec 27, 2018
570cd78
Merge branch 'master' into typedarray_parser
jerch Jan 2, 2019
83c734e
Merge branch 'typedarray_parser' of git+ssh://github.com/jerch/xterm.…
jerch Jan 2, 2019
5fd764a
dont pullin everything from .vscode folder
jerch Jan 2, 2019
dfc853a
add TextDecoder for string to UTF32
jerch Jan 2, 2019
2918a67
fix wrongly changed test case
jerch Jan 2, 2019
8fc66a3
switch parse buffer to UTF32
jerch Jan 2, 2019
1fb1f55
Merge branch 'master' into typedarray_parser
jerch Jan 2, 2019
27b91f6
speedup utf32ToString conversion
jerch Jan 3, 2019
9bcf06f
Merge branch 'master' into typedarray_parser
jerch Jan 5, 2019
e7e0f67
Merge branch 'master' into typedarray_parser
jerch Jan 12, 2019
a1f8749
Merge branch 'typedarray_parser' of git+ssh://github.com/jerch/xterm.…
jerch Jan 12, 2019
329d40a
cleanup DCS handler
jerch Jan 12, 2019
9549896
Merge branch 'master' into typedarray_parser
jerch Jan 17, 2019
19931e7
Merge branch 'master' into typedarray_parser
jerch Jan 25, 2019
21165a4
Merge branch 'master' into typedarray_parser
jerch Jan 25, 2019
4855b60
move utf32ToString to TextDecoder.ts
jerch Jan 25, 2019
87e9789
more explicit docs for DCS parsers
jerch Jan 25, 2019
90a41a1
review changes
jerch Jan 27, 2019
8e37883
Merge branch 'master' into typedarray_parser
Tyriar Jan 27, 2019
e67bef6
cleanup types
jerch Jan 27, 2019
fdf784e
Merge branch 'master' into typedarray_parser
jerch Jan 27, 2019
975dfc4
Merge branch 'typedarray_parser' of git+ssh://github.com/jerch/xterm.…
jerch Jan 27, 2019
358d70d
cleanup gitignore
jerch Jan 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { wcwidth } from './CharWidth';
import { EscapeSequenceParser } from './EscapeSequenceParser';
import { ICharset } from './core/Types';
import { Disposable } from './common/Lifecycle';
import { concat, utf16ToString } from './common/TypedArrayUtils';

/**
* Map collect to glevel. Used in `selectCharset`.
Expand All @@ -33,22 +34,16 @@ const GLEVEL: {[key: string]: number} = {'(': 0, ')': 1, '*': 2, '+': 3, '-': 1,
private _data: Uint16Array = new Uint16Array(0);
constructor(private _terminal: any) { }
hook(collect: string, params: number[], flag: number): void {
this._data = new Uint16Array(0);
}
put(data: Uint16Array, start: number, end: number): void {
const tmp = new Uint16Array(this._data.length + end - start);
tmp.set(this._data);
tmp.set(data.subarray(start, end), this._data.length);
this._data = data;
this._data = concat(this._data, data.subarray(start, end));
}
unhook(): void {
let data = '';
for (let i = 0; i < this._data.length; ++i) {
data += String.fromCharCode(this._data[i]);
}
this._data = new Uint16Array(0); // dont hold memory longer than needed
const data = utf16ToString(this._data);
this._data = new Uint16Array(0);
// invalid: DCS 0 + r Pt ST
this._terminal.handler(`${C0.ESC}P0+r${data}${C0.ESC}\\`);
this._data = new Uint16Array(0);
}
}

Expand All @@ -67,18 +62,12 @@ class DECRQSS implements IDcsHandler {
}

put(data: Uint16Array, start: number, end: number): void {
const tmp = new Uint16Array(this._data.length + end - start);
tmp.set(this._data);
tmp.set(data.subarray(start, end), this._data.length);
this._data = data;
this._data = concat(this._data, data.subarray(start, end));
}

unhook(): void {
let data = '';
for (let i = 0; i < this._data.length; ++i) {
data += String.fromCharCode(this._data[i]);
}
this._data = new Uint16Array(0); // dont hold memory longer than needed
const data = utf16ToString(this._data);
this._data = new Uint16Array(0);
switch (data) {
// valid: DCS 1 $ r Pt ST (xterm)
case '"q': // DECSCA
Expand Down
32 changes: 24 additions & 8 deletions src/common/TypedArrayUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
* @license MIT
*/
import { assert } from 'chai';
import { fillFallback } from './TypedArrayUtils';
import { fillFallback, concat, utf16ToString } from './TypedArrayUtils';

type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray
| Int8Array | Int16Array | Int32Array
| Float32Array | Float64Array;

describe('polyfill conformance tests', function(): void {

function deepEquals(a: TypedArray, b: TypedArray): void {
assert.equal(a.length, b.length);
for (let i = 0; i < a.length; ++i) {
assert.equal(a[i], b[i]);
}
function deepEquals(a: TypedArray, b: TypedArray): void {
assert.equal(a.length, b.length);
for (let i = 0; i < a.length; ++i) {
assert.equal(a[i], b[i]);
}
}

describe('polyfill conformance tests', function(): void {
describe('TypedArray.fill', function(): void {
it('should work with all typed array types', function(): void {
const u81 = new Uint8Array(5);
Expand Down Expand Up @@ -87,3 +86,20 @@ describe('polyfill conformance tests', function(): void {
});
});
});

describe('typed array convenience functions', () => {
it('concat', () => {
const a = new Uint8Array([1, 2, 3, 4, 5]);
const b = new Uint8Array([6, 7, 8, 9, 0]);
const merged = concat(a, b);
deepEquals(merged, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));
});
it('utf16ToString', () => {
const s = 'abcdefg';
const data = new Uint16Array(s.length);
for (let i = 0; i < s.length; ++i) {
data[i] = s.charCodeAt(i);
}
assert.equal(utf16ToString(data), s);
});
});
34 changes: 29 additions & 5 deletions src/common/TypedArrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* @license MIT
*/

/**
* polyfill for TypedArray.fill
* This is needed to support .fill in all safari versions and IE 11.
*/

type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray
| Int8Array | Int16Array | Int32Array
| Float32Array | Float64Array;

/**
* polyfill for TypedArray.fill
* This is needed to support .fill in all safari versions and IE 11.
*/
export function fill<T extends TypedArray>(array: T, value: number, start?: number, end?: number): T {
// all modern engines that support .fill
if (array.fill) {
Expand Down Expand Up @@ -39,3 +38,28 @@ export function fillFallback<T extends TypedArray>(array: T, value: number, star
}
return array;
}

/**
* Concat two typed arrays `a` and `b`.
* Returns a new typed array.
*/
export function concat<T extends TypedArray>(a: T, b: T): T {
const result = new (a.constructor as any)(a.length + b.length);
jerch marked this conversation as resolved.
Show resolved Hide resolved
result.set(a);
result.set(b, a.length);
return result;
}

/**
* Convert UTF16 char codes into JS string.
* Note the typed array is not limited to Uint16Array, make sure to align
* the values to 0-65535 integer for other typed array types, otherwise
* the conversion will fail.
*/
export function utf16ToString<T extends TypedArray>(data: T): string {
let s = '';
for (let i = 0; i < data.length; ++i) {
s += String.fromCharCode(data[i]);
jerch marked this conversation as resolved.
Show resolved Hide resolved
}
return s;
}