|
| 1 | +/* |
| 2 | + * RegExp.prototype.compile() polyfill, described in E6 Annex B: |
| 3 | + * |
| 4 | + * https://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.compile |
| 5 | + * |
| 6 | + * See also: |
| 7 | + * |
| 8 | + * http://mozilla.6506.n7.nabble.com/RegExp-prototype-compile-and-RegExp-instance-properties-td270408.html |
| 9 | + * |
| 10 | + * This polyfill cannot be implemented in terms of standard E5 because it |
| 11 | + * needs to reinitialize the internal state of a RegExp instance. To do |
| 12 | + * that, we access the Duktape internal properties directly, which is |
| 13 | + * quite fragile. |
| 14 | + * |
| 15 | + * Avoid storing a public copy of Duktape or some internal property name in |
| 16 | + * the global object. This could subvert user sandboxing. |
| 17 | + */ |
| 18 | + |
| 19 | +(function () { |
| 20 | + var propBytecode = Duktape.dec('hex', 'ff62797465636f6465'); // \xFFbytecode |
| 21 | + |
| 22 | + if (typeof RegExp.prototype.compile !== 'undefined') { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + Object.defineProperty(RegExp.prototype, 'compile', { |
| 27 | + value: function (pattern, flags) { |
| 28 | + var newBytecode, tmpRegexp; |
| 29 | + if (typeof this !== 'object' || !(propBytecode in this)) { |
| 30 | + throw new TypeError('invalid this binding'); |
| 31 | + } |
| 32 | + |
| 33 | + // FIXME: property attributes prevent this approach from working |
| 34 | + // right now. The properties we'd need to modify are non-writable |
| 35 | + // and non-configurable: |
| 36 | + // |
| 37 | + // \xffbytecode |
| 38 | + // source |
| 39 | + // global |
| 40 | + // ignoreCase |
| 41 | + // multiline |
| 42 | + // |
| 43 | + // The property attributes can be relaxed, or the properties can |
| 44 | + // be made accessors backing to the regexp bytecode, see Ditz |
| 45 | + // issues: 0f2c246cadbb3b3913b75dc7e890ee4e7d336a1a and |
| 46 | + // f8396fbcc36db4610fec5ad5a7d7a8f471d084a4. |
| 47 | + |
| 48 | + if (typeof pattern === 'object' && (propBytecode in pattern)) { |
| 49 | + this[propBytecode] = pattern[propBytecode]; |
| 50 | + } else { |
| 51 | + tmpRegexp = new RegExp(pattern, flags); |
| 52 | + this[propBytecode] = tmpRegexp[propBytecode]; |
| 53 | + } |
| 54 | + |
| 55 | + //this.source |
| 56 | + //this.global |
| 57 | + //this.ignoreCase |
| 58 | + //this.multiline |
| 59 | + this.lastIndex = 0; |
| 60 | + return this; |
| 61 | + }, writable: true, enumerable: false, configurable: true |
| 62 | + }); |
| 63 | + |
| 64 | + throw new Error('RegExp.prototype.compile() polyfill incomplete'); |
| 65 | +})(); |
0 commit comments