~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/runtime.js

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    sep = sep || ';';
26
26
    var ret = RuntimeGenerator.alloc(size, 'STACK', false, sep, USE_TYPED_ARRAYS != 2 || (isNumber(size) && parseInt(size) % {{{ STACK_ALIGN }}} == 0));
27
27
    if (ASSERTIONS) {
28
 
      ret += sep + 'assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')';
 
28
      ret += sep + '(assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')|0)';
29
29
    }
30
30
    return ret;
31
31
  },
32
32
 
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)';
41
41
      }
42
42
    }
43
43
    if (ASSERTIONS) {
44
 
      ret += '; assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')';
 
44
      ret += '; (assert(' + asmCoercion('(STACKTOP|0) < (STACK_MAX|0)', 'i32') + ')|0)';
45
45
    }
46
46
    if (false) {
47
 
      ret += '; _memset(' + asmCoercion('__stackBase__', 'i32') + ', 0, ' + initial + ')';
 
47
      ret += '; _memset(' + asmCoercion('sp', 'i32') + ', 0, ' + initial + ')';
48
48
    }
49
49
    return ret;
50
50
  },
53
53
    if (initial === 0 && SKIP_STACK_IN_SMALL && !force) return '';
54
54
    var ret = '';
55
55
    if (SAFE_HEAP) {
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 }';
57
57
    }
58
 
    return ret += 'STACKTOP = __stackBase__';
 
58
    return ret += 'STACKTOP = sp';
59
59
  },
60
60
 
61
61
  // An allocation that cannot normally be free'd (except through sbrk, which once
201
201
    type.alignSize = 0;
202
202
    var diffs = [];
203
203
    var prev = -1;
 
204
    var index = 0;
204
205
    type.flatIndexes = type.fields.map(function(field) {
 
206
      index++;
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!');
 
216
          size = 0;
 
217
          if (Types.types[field]) {
 
218
            alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
 
219
          } else {
 
220
            alignSize = type.alignSize || QUANTUM_SIZE;
 
221
          }
 
222
        } else {
 
223
          size = Types.types[field].flatSize;
 
224
          alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
 
225
        }
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;
376
390
 
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() {
381
396
    var buffer = [];
382
397
    var needed = 0;
383
398
    this.processCChar = function (code) {
384
 
      code = code & 0xff;
385
 
      if (needed) {
386
 
        buffer.push(code);
387
 
        needed--;
388
 
      }
 
399
      code = code & 0xFF;
 
400
 
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);
 
404
        }
391
405
        buffer.push(code);
392
 
        if (code > 191 && code < 224) {
 
406
        if ((code & 0xE0) == 0xC0) {        // 110xxxxx
393
407
          needed = 1;
394
 
        } else {
 
408
        } else if ((code & 0xF0) == 0xE0) { // 1110xxxx
395
409
          needed = 2;
 
410
        } else {                            // 11110xxx
 
411
          needed = 3;
396
412
        }
397
413
        return '';
398
414
      }
399
 
      if (needed > 0) return '';
 
415
 
 
416
      if (needed) {
 
417
        buffer.push(code);
 
418
        needed--;
 
419
        if (needed > 0) return '';
 
420
      }
 
421
 
400
422
      var c1 = buffer[0];
401
423
      var c2 = buffer[1];
402
424
      var c3 = buffer[2];
 
425
      var c4 = buffer[3];
403
426
      var ret;
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));
406
431
      } else {
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);
408
438
      }
409
439
      buffer.length = 0;
410
440
      return ret;