hecht-software / box2dweb

Automatically exported from code.google.com/p/box2dweb
308 stars 94 forks source link

redefining defineProperty breaks on chrome #43

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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 marcogil...@gmail.com on 24 Jun 2013 at 9:31

Attachments:

aztack commented 6 years ago

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

danzen commented 2 years ago

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