26
26
var ret = RuntimeGenerator.alloc(size, 'STACK', false, sep, USE_TYPED_ARRAYS != 2 || (isNumber(size) && parseInt(size) % {{{ STACK_ALIGN }}} == 0));
28
ret += sep + 'assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')';
28
ret += sep + '(assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')|0)';
33
33
stackEnter: function(initial, force) {
34
34
if (initial === 0 && SKIP_STACK_IN_SMALL && !force) return '';
35
var ret = 'var __stackBase__ = ' + (ASM_JS ? '0; __stackBase__ = ' : '') + 'STACKTOP';
35
var ret = 'var sp = ' + (ASM_JS ? '0; sp = ' : '') + 'STACKTOP';
36
36
if (initial > 0) ret += '; STACKTOP = (STACKTOP + ' + initial + ')|0';
37
37
if (USE_TYPED_ARRAYS == 2) {
38
38
assert(initial % Runtime.STACK_ALIGN == 0);
39
39
if (ASSERTIONS && Runtime.STACK_ALIGN == 4) {
40
ret += '; assert(' + asmCoercion('!(STACKTOP&3)', 'i32') + ')';
40
ret += '; (assert(' + asmCoercion('!(STACKTOP&3)', 'i32') + ')|0)';
44
ret += '; assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')';
44
ret += '; (assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')|0)';
47
ret += '; _memset(' + asmCoercion('__stackBase__', 'i32') + ', 0, ' + initial + ')';
47
ret += '; _memset(' + asmCoercion('sp', 'i32') + ', 0, ' + initial + ')';
53
53
if (initial === 0 && SKIP_STACK_IN_SMALL && !force) return '';
56
ret += 'var i = __stackBase__; while ((i|0) < (STACKTOP|0)) { SAFE_HEAP_CLEAR(i|0); i = (i+1)|0 }';
56
ret += 'var i = sp; while ((i|0) < (STACKTOP|0)) { SAFE_HEAP_CLEAR(i|0); i = (i+1)|0 }';
58
return ret += 'STACKTOP = __stackBase__';
58
return ret += 'STACKTOP = sp';
61
61
// An allocation that cannot normally be free'd (except through sbrk, which once
201
201
type.alignSize = 0;
204
205
type.flatIndexes = type.fields.map(function(field) {
205
207
var size, alignSize;
206
208
if (Runtime.isNumberType(field) || Runtime.isPointerType(field)) {
207
209
size = Runtime.getNativeTypeSize(field); // pack char; char; in structs, also char[X]s.
208
210
alignSize = Runtime.getAlignSize(field, size);
209
211
} else if (Runtime.isStructType(field)) {
210
size = Types.types[field].flatSize;
211
alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
212
if (field[1] === '0') {
213
// this is [0 x something]. When inside another structure like here, it must be at the end,
214
// and it adds no size
215
// XXX this happens in java-nbody for example... assert(index === type.fields.length, 'zero-length in the middle!');
217
if (Types.types[field]) {
218
alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
220
alignSize = type.alignSize || QUANTUM_SIZE;
223
size = Types.types[field].flatSize;
224
alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
212
226
} else if (field[0] == 'b') {
213
227
// bN, large number field, like a [N x i8]
214
228
size = field.substr(1)|0;
377
391
// Returns a processor of UTF.
378
392
// processCChar() receives characters from a C-like UTF representation and returns JS string fragments.
393
// See RFC3629 for details, the bytes are assumed to be valid UTF-8
379
394
// processJSString() receives a JS string and returns a C-like UTF representation in an array
380
395
UTF8Processor: function() {
383
398
this.processCChar = function (code) {
389
401
if (buffer.length == 0) {
390
if (code < 128) return String.fromCharCode(code);
402
if ((code & 0x80) == 0x00) { // 0xxxxxxx
403
return String.fromCharCode(code);
391
405
buffer.push(code);
392
if (code > 191 && code < 224) {
406
if ((code & 0xE0) == 0xC0) { // 110xxxxx
408
} else if ((code & 0xF0) == 0xE0) { // 1110xxxx
399
if (needed > 0) return '';
419
if (needed > 0) return '';
400
422
var c1 = buffer[0];
401
423
var c2 = buffer[1];
402
424
var c3 = buffer[2];
404
if (c1 > 191 && c1 < 224) {
405
ret = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
427
if (buffer.length == 2) {
428
ret = String.fromCharCode(((c1 & 0x1F) << 6) | (c2 & 0x3F));
429
} else if (buffer.length == 3) {
430
ret = String.fromCharCode(((c1 & 0x0F) << 12) | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
407
ret = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
432
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
433
var codePoint = ((c1 & 0x07) << 18) | ((c2 & 0x3F) << 12) |
434
((c3 & 0x3F) << 6) | (c4 & 0x3F);
435
ret = String.fromCharCode(
436
Math.floor((codePoint - 0x10000) / 0x400) + 0xD800,
437
(codePoint - 0x10000) % 0x400 + 0xDC00);
409
439
buffer.length = 0;