[quickjs-devel] Failure to override String.length ?

  • From: Saúl Ibarra Corretgé <s@xxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Sun, 13 Jun 2021 22:10:05 +0200

Hey folks,

While running some code through QuickJS I ran into a difference in behaviour with regards to V8 and SpiderMonkey.

This code:

"use strict";

function SafeString(val) {
  if (typeof val !== 'string') {
    return val;
  }

  this.val = val;
  this.length = val.length;
}

SafeString.prototype = Object.create(String.prototype, {
  length: {
    writable: true,
    configurable: true,
    value: 0
  }
});

SafeString.prototype.valueOf = function valueOf() {
  return this.val;
};

SafeString.prototype.toString = function toString() {
  return this.val;
};

const s = new SafeString('test123');

console.log(s);

Works on V8 and SpiderMonkey but fails with this error on QuickJS:

TypeError: 'length' is read-only
    at SafeString (testString.js:9)
    at <eval> (testString.js:28)

Looks like the length property is not overriden properly?


Cheers,

--
Saúl

Attachment: testString.js
Description: JavaScript source

Other related posts: