-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (75 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var Class = function(upper, init) {
var toString = Object.prototype.toString
var slice = [].slice
function isFunction(f) {
return toString.call(f) == '[object Function]'
}
function inherit(cls, upper) {
function F(){}
F.prototype = upper.prototype
cls.prototype = new F()
cls.prototype.constructor = cls
cls.__parent__ = cls.prototype.__parent__ = upper
// copy static members
for (var prop in upper) {
if (upper.hasOwnProperty(prop)) {
cls[prop] = upper[prop]
}
}
}
if (arguments.length == 1) {
init = upper
upper = Object
}
var cls = function() {
// __init__ as initializer
this.__init__ && this.__init__.apply(this, arguments)
}
upper && inherit(cls, upper)
var obj = {}
isFunction(init) && init.call(obj)
function wrap(name, func) {
if (!isFunction(func)) {
return func
}
var wrapped = function() {
var args = slice.call(arguments)
// set "this" as the first param
args.unshift(this)
return func.apply(this, args)
}
// remember func name, for finding
func.__name__ = wrapped.__name__ = name
return wrapped
}
// static members in __static__
var statics = obj['__static__']
if (statics) {
for(var prop in statics) {
cls[prop] = statics[prop]
}
delete obj['__static__']
}
for (var prop in obj) {
if (prop == 'parent') {
console.error('[PyClass Error] parent is preserved word for pyclass');
continue;
}
if (obj.hasOwnProperty(prop)) {
// wrap function, add self as the first param
cls.prototype[prop] = wrap(prop, obj[prop])
}
}
// make self.parent() available
cls.prototype.parent = function() {
var name = arguments.callee.caller.__name__
if (!name) {
return console.error('[PyClass Error] parent() should not be called in nested function')
}
this.__parent__.prototype[name].apply(this, arguments)
}
return cls
}
if (typeof module != 'undefined') {
module.exports = Class
}