-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/es6 class constructor support #182
Open
OJezu
wants to merge
3
commits into
cujojs:master
Choose a base branch
from
OJezu:feature/es6-class-constructor-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+709
−14,250
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ node_modules/ | |
experiments/ | ||
.idea/ | ||
*~ | ||
*.log | ||
bower_components/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Testing wire.js | ||
|
||
Wire.js is using [buster](http://busterjs.org) for testing. You need to have [node.js](https://nodejs.org) installed to run tests of wire.js. | ||
|
||
## Testing in node.js: | ||
|
||
[Install wire.js](docs/get.md) and run in installation directory | ||
``` | ||
$ npm install | ||
$ npm test | ||
``` | ||
|
||
## Testing in browser | ||
|
||
[Install wire.js](docs/get.md) and run in installation directory | ||
|
||
``` | ||
$ npm install | ||
$ npm run-script start-test-server | ||
``` | ||
|
||
Open http://localhost:1111 in your browser and click "Capture browser" button. Browser is now connected to test server | ||
and will be used by it to run tests. | ||
|
||
Run in wire.js installation directory (without closing browser and test server) | ||
``` | ||
$ npm run-script browser-test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function(define) { | ||
'use strict'; | ||
define(function() { | ||
/** | ||
* Carefully sets the instance's constructor property to the supplied | ||
* constructor, using Object.defineProperty if available. If it can't | ||
* set the constructor in a safe way, it will do nothing. | ||
* | ||
* @param instance {Object} component instance | ||
* @param ctor {Function} constructor | ||
*/ | ||
function defineConstructorIfPossible(instance, ctor) { | ||
try { | ||
Object.defineProperty(instance, 'constructor', { | ||
value: ctor, | ||
enumerable: false | ||
}); | ||
} catch(e) { | ||
// If we can't define a constructor, oh well. | ||
// This can happen if in envs where Object.defineProperty is not | ||
// available, or when using cujojs/poly or other ES5 shims | ||
} | ||
} | ||
|
||
return function(func, thisObj, args) { | ||
var result = null; | ||
|
||
if(thisObj && typeof thisObj[func] === 'function') { | ||
func = thisObj[func]; | ||
} | ||
|
||
// detect case when apply is called on constructor and fix prototype chain | ||
if (thisObj === func) { | ||
thisObj = Object.create(func.prototype); | ||
defineConstructorIfPossible(thisObj, func); | ||
func.apply(thisObj, args); | ||
result = thisObj; | ||
} else { | ||
result = func.apply(thisObj, args); | ||
} | ||
|
||
return result; | ||
}; | ||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* jshint esversion: 6 */ | ||
(function(define) { | ||
'use strict'; | ||
define(function() { | ||
return function(func, thisObj, args) { | ||
var result = null; | ||
|
||
if(thisObj === func || (thisObj && thisObj.constructor === func)) { | ||
/* jshint newcap: false */ | ||
result = new func(...(args||[])); | ||
|
||
// detect broken old prototypes with missing constructor | ||
if (result.constructor !== func) { | ||
Object.defineProperty(result, 'constructor', { | ||
enumerable: false, | ||
value: func | ||
}); | ||
} | ||
} else if(thisObj && typeof thisObj[func] === 'function') { | ||
result = thisObj[func](...args); | ||
} else { | ||
result = func.apply(thisObj, args); | ||
} | ||
|
||
return result; | ||
}; | ||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ | |
*/ | ||
|
||
(function(define){ 'use strict'; | ||
define(function() { | ||
define(function(require) { | ||
|
||
var undef; | ||
var undef, universalApply; | ||
|
||
universalApply = require('./universalApply'); | ||
|
||
/** | ||
* Creates an object by either invoking ctor as a function and returning the result, | ||
|
@@ -26,42 +28,19 @@ define(function() { | |
var begotten, ctorResult; | ||
|
||
if (forceConstructor || (forceConstructor === undef && isConstructor(ctor))) { | ||
begotten = Object.create(ctor.prototype); | ||
defineConstructorIfPossible(begotten, ctor); | ||
ctorResult = ctor.apply(begotten, args); | ||
begotten = ctor; | ||
ctorResult = universalApply(ctor, begotten, args); | ||
|
||
if(ctorResult !== undef) { | ||
begotten = ctorResult; | ||
} | ||
|
||
} else { | ||
begotten = ctor.apply(undef, args); | ||
|
||
begotten = universalApply(ctor, undef, args); | ||
} | ||
|
||
return begotten === undef ? null : begotten; | ||
}; | ||
|
||
/** | ||
* Carefully sets the instance's constructor property to the supplied | ||
* constructor, using Object.defineProperty if available. If it can't | ||
* set the constructor in a safe way, it will do nothing. | ||
* | ||
* @param instance {Object} component instance | ||
* @param ctor {Function} constructor | ||
*/ | ||
function defineConstructorIfPossible(instance, ctor) { | ||
try { | ||
Object.defineProperty(instance, 'constructor', { | ||
value: ctor, | ||
enumerable: false | ||
}); | ||
} catch(e) { | ||
// If we can't define a constructor, oh well. | ||
// This can happen if in envs where Object.defineProperty is not | ||
// available, or when using cujojs/poly or other ES5 shims | ||
} | ||
} | ||
|
||
/** | ||
* Determines whether the supplied function should be invoked directly or | ||
* should be invoked using new in order to create the object to be wired. | ||
|
@@ -72,10 +51,17 @@ define(function() { | |
*/ | ||
function isConstructor(func) { | ||
var is = false, p; | ||
for (p in func.prototype) { | ||
if (p !== undef) { | ||
is = true; | ||
break; | ||
|
||
// this has to work, according to spec: | ||
// https://tc39.github.io/ecma262/#sec-function.prototype.tostring | ||
is = is || func.toString().trim().substr(0,5) === 'class'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, looks right. Nice find. Any idea of the performance characteristics of this? I figure it's probably fine, but just want to make sure we're not introducing a big perf hit by |
||
|
||
if(!is) { | ||
for (p in func.prototype) { | ||
if (p !== undef) { | ||
is = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -88,6 +74,6 @@ define(function() { | |
? define | ||
// CommonJS | ||
: function(factory) { | ||
module.exports = factory(); | ||
module.exports = factory(require); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
(function(define) { | ||
define(function() { | ||
define(function(require) { | ||
|
||
var universalApply = require('./universalApply'); | ||
|
||
return function(methodName, args) { | ||
return function(target) { | ||
return target[methodName].apply(target, args); | ||
return universalApply(target[methodName], target, args); | ||
}; | ||
}; | ||
|
||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function(){ | ||
'use strict'; | ||
|
||
(function(define){ | ||
|
||
function evaluates (statement) { | ||
try { | ||
/* jshint evil: true */ | ||
eval(statement); | ||
/* jshint evil: false */ | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
// we have to know it synchronously, we are unable to load this module in asynchronous way | ||
// we cannot defer `define` and we cannot load module, that would not compile in browser | ||
// so we can't delegate this check to another module | ||
function isSpreadAvailable() { | ||
return evaluates('Math.max(...[ 5, 10 ])'); | ||
} | ||
|
||
var requires = []; | ||
if (typeof(process) !== 'undefined' && 'ES_VERSION' in process.env) { | ||
requires.push('./es'+process.env.ES_VERSION+'Apply'); | ||
} else { | ||
if(isSpreadAvailable()) { | ||
requires.push('./es6Apply'); | ||
} else { | ||
requires.push('./es5Apply'); | ||
} | ||
} | ||
|
||
define(requires, function(apply){ | ||
return apply; | ||
}); | ||
})( | ||
typeof define === 'function' | ||
? define | ||
: function(requires, factory) { | ||
module.exports = factory.apply(null, requires.map(require)); | ||
} | ||
); | ||
})(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to modify the prototype from here, so I set the constructor property on object itself.