OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 let { |
| 4 Runtime, AsciiToString, HEAP16, HEAP32, _free, _getException |
| 5 } = require("./compiled"); |
| 6 |
| 7 function createClass(superclass) |
| 8 { |
| 9 let result = function(pointer) |
| 10 { |
| 11 this._pointer = pointer; |
| 12 }; |
| 13 if (superclass) |
| 14 result.prototype = Object.create(superclass.prototype); |
| 15 return result; |
| 16 } |
| 17 exports.createClass = createClass; |
| 18 |
| 19 function wrapCall(call) |
| 20 { |
| 21 return function() |
| 22 { |
| 23 let sp = Runtime.stackSave(); |
| 24 try |
| 25 { |
| 26 return call.apply(this, arguments); |
| 27 } |
| 28 catch (e) |
| 29 { |
| 30 throw typeof e == "number" ? new Error(AsciiToString(_getException(e))) :
e; |
| 31 } |
| 32 finally |
| 33 { |
| 34 Runtime.stackRestore(sp); |
| 35 } |
| 36 }; |
| 37 } |
| 38 exports.wrapCall = wrapCall; |
| 39 |
| 40 function convertString(str) |
| 41 { |
| 42 let length = str.length; |
| 43 let buffer = Runtime.stackAlloc(length * 2); |
| 44 for (let i = 0, pointer = (buffer >> 1); i < length; i++, pointer++) |
| 45 HEAP16[pointer] = str.charCodeAt(i); |
| 46 return buffer; |
| 47 } |
| 48 |
| 49 function stringArg(argNumber, call) |
| 50 { |
| 51 return function() |
| 52 { |
| 53 let params = Array.slice(arguments); |
| 54 let str = params[argNumber]; |
| 55 if (str != null) |
| 56 params.splice(argNumber, 1, convertString(str), str.length); |
| 57 else |
| 58 params.splice(argNumber, 1, 0, 0); |
| 59 return call.apply(this, params); |
| 60 }; |
| 61 } |
| 62 exports.stringArg = stringArg; |
| 63 |
| 64 function stringResult(call) |
| 65 { |
| 66 return function() |
| 67 { |
| 68 let params = Array.slice(arguments); |
| 69 let resultLen = Runtime.stackAlloc(4); |
| 70 params.push(resultLen); |
| 71 |
| 72 let buffer = call.apply(this, params); |
| 73 if (buffer) |
| 74 { |
| 75 let length = HEAP32[resultLen >> 2]; |
| 76 let pointer = buffer >> 1; |
| 77 let str = String.fromCharCode.apply(String, HEAP16.slice(pointer, pointer
+ length)); |
| 78 _free(buffer); |
| 79 return str; |
| 80 } |
| 81 else |
| 82 return null; |
| 83 }; |
| 84 } |
| 85 exports.stringResult = stringResult; |
| 86 |
| 87 function _convertGetter(call) |
| 88 { |
| 89 return function() |
| 90 { |
| 91 return call(this._pointer); |
| 92 }; |
| 93 } |
| 94 |
| 95 function _convertSetter(call) |
| 96 { |
| 97 return function(value) |
| 98 { |
| 99 call(this._pointer, value); |
| 100 } |
| 101 } |
| 102 |
| 103 function defineProperty(cls, name, getter, setter) |
| 104 { |
| 105 let descriptor = |
| 106 { |
| 107 enumerable: true, |
| 108 get: wrapCall(_convertGetter(getter)) |
| 109 }; |
| 110 if (setter) |
| 111 descriptor.set = wrapCall(_convertSetter(setter)); |
| 112 Object.defineProperty(cls.prototype, name, descriptor); |
| 113 } |
| 114 exports.defineProperty = defineProperty; |
| 115 |
| 116 function defineStringProperty(cls, name, getter, setter) |
| 117 { |
| 118 defineProperty(cls, name, stringResult(getter), |
| 119 (setter ? stringArg(1, setter) : null)); |
| 120 } |
| 121 exports.defineStringProperty = defineStringProperty; |
OLD | NEW |