mirror of
https://github.com/flynx/object.js.git
synced 2025-10-29 18:40:08 +00:00
reworked constructor introspection...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
ff63091784
commit
9d31f9f985
45
object.js
45
object.js
@ -186,9 +186,48 @@ function makeConstructor(name, a, b){
|
||||
.replace(/Constructor/g, name))
|
||||
}
|
||||
|
||||
// XXX it turns out we can't directly assign to .constructor.prototype
|
||||
// and by assigning directly to .__proto__ we lose some meta
|
||||
// information like function name...
|
||||
// set an informative .toString...
|
||||
// NOTE: do this only if .toString(..) is not defined by user...
|
||||
if(cls_proto
|
||||
&& cls_proto.toString() == ({}).toString()){
|
||||
// XXX is this the right way to go or should we set this openly???
|
||||
Object.defineProperty(_constructor, 'toString', {
|
||||
value: function(){
|
||||
var args = proto.__init__ ?
|
||||
proto.__init__
|
||||
.toString()
|
||||
.split(/\n/)[0].replace(/function\(([^)]*)\){.*/, '$1')
|
||||
: ''
|
||||
var code = proto.__init__ ?
|
||||
proto.__init__
|
||||
.toString()
|
||||
.replace(/[^{]*{/, '{')
|
||||
: '{ .. }'
|
||||
|
||||
// normalize code indent...
|
||||
// NOTE: this repeats ImageGrid's core.doc(..)...
|
||||
var lines = code.split(/\n/)
|
||||
var l = lines
|
||||
.reduce(function(l, e, i){
|
||||
var indent = e.length - e.trimLeft().length
|
||||
return e.trim().length == 0
|
||||
// ignore 0 indent of first line...
|
||||
|| (i == 0 && indent == 0) ? l
|
||||
: l < 0 ?
|
||||
indent
|
||||
: Math.min(l, indent)
|
||||
}, -1)
|
||||
code = lines
|
||||
.map(function(line, i){
|
||||
return i == 0 ? line : line.slice(l) })
|
||||
.join('\n')
|
||||
|
||||
return `${this.name}(${args})${code}`
|
||||
},
|
||||
enumerable: false,
|
||||
})
|
||||
}
|
||||
|
||||
_constructor.__proto__ = cls_proto
|
||||
_constructor.prototype = proto
|
||||
_constructor.prototype.constructor = _constructor
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-object",
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.5",
|
||||
"description": "",
|
||||
"main": "object.js",
|
||||
"scripts": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user