box2dweb icon indicating copy to clipboard operation
box2dweb copied to clipboard

redefining defineProperty breaks on chrome

Open GoogleCodeExporter opened this issue 10 years ago • 2 comments

What steps will reproduce the problem?
1. run a box2dweb app on chrome
2. call (new Date).toLocaleTimeString();
3. (my test code is a bit more complex as it went through processing.js). I 
think I know the solution, so haven't produced a particularly simple test case, 
but can do if the problem remains

What is the expected output? What do you see instead?

(new Date).toLocaleTimeString(); produces an error: 

Error: Internal error. Formatter and date value have to be specified. 


Please provide any additional information below.


After hunting around a lot, I think this is because this code attempts to 
redefine Object.defineProperty. 

console.log(Object.defineProperty);
   if(!(Object.prototype.defineProperty instanceof Function)
      && Object.prototype.__defineGetter__ instanceof Function
      && Object.prototype.__defineSetter__ instanceof Function)
   {
      Object.defineProperty = function(obj, p, cfg) {
         if(cfg.get instanceof Function)
            obj.__defineGetter__(p, cfg.get);
         if(cfg.set instanceof Function)
            obj.__defineSetter__(p, cfg.set);
      }
   }

It ought to only do it when it does not exist, but in recent Chrome, 
defineProperty is a native function which seems not to appear in the prototype. 

I think I have fixed it by adding a check against Object.defineProperty:

if((!(Object.prototype.defineProperty instanceof Function)
      && !(Object.defineProperty instanceof Function))
      && Object.prototype.__defineGetter__ instanceof Function
      && Object.prototype.__defineSetter__ instanceof Function)
   {
      Object.defineProperty = function(obj, p, cfg) {
         if(cfg.get instanceof Function)
            obj.__defineGetter__(p, cfg.get);
         if(cfg.set instanceof Function)
            obj.__defineSetter__(p, cfg.set);
      }
   }

Original issue reported on code.google.com by [email protected] on 24 Jun 2013 at 9:31

Attachments:

GoogleCodeExporter avatar Aug 24 '15 10:08 GoogleCodeExporter

box2dweb.js in Egret lakeshore generated code still includes this bug. Above fix(check against Object.defineProperty) will fix it.

aztack avatar Aug 07 '17 08:08 aztack

I concur - solved it and came to report it and found this - hahaha - six years later ;-).

danzen avatar Mar 01 '22 13:03 danzen