Skip to content

Commit e8c9b6b

Browse files
committed
support ES5 engines
1 parent 083c9cd commit e8c9b6b

2 files changed

Lines changed: 91 additions & 28 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@kaoscript/runtime",
33
"description": "The default runtime for kaoscript",
4-
"version": "0.4.1",
4+
"version": "0.5.0",
55
"author": {
66
"name": "Baptiste Augrain",
77
"email": "daiyam@zokugun.org"
@@ -20,7 +20,7 @@
2020
}
2121
],
2222
"engines": {
23-
"node": ">= 6.0.0"
23+
"node": ">= 4.0.0"
2424
},
2525
"main": "src/runtime.js",
2626
"files": [

src/runtime.js

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* runtime.js
3-
* Version 0.4.1
3+
* Version 0.5.0
44
* September 14th, 2016
55
*
66
* Copyright (c) 2016 Baptiste Augrain
@@ -138,10 +138,7 @@ var $helper = {
138138

139139
return null;
140140
}, // }}}
141-
methods: function(variable, name, parameters, methods, call, argName, refName, returns) { // {{{
142-
//var source = 'console.log("' + name + '", arguments);if(false){}';
143-
var source = '';
144-
141+
methods: function(variable, name, parameters, source, methods, call, argName, refName, returns) { // {{{
145142
var method;
146143
if(methods.length === 0) {
147144
source += 'if(' + argName + '.length !== 0) {';
@@ -217,14 +214,18 @@ var $helper = {
217214
}
218215

219216
var nf = true;
217+
var we = false
220218

221219
for(var g = 0; g < groups.length; g++) {
222220
group = groups[g];
223221

224222
if(group.min === group.max) {
225-
if(source.length) {
223+
if(we) {
226224
source += ' else '
227225
}
226+
else {
227+
we = true;
228+
}
228229

229230
source += 'if(' + argName + '.length === ' + group.min + ') {';
230231

@@ -238,9 +239,12 @@ var $helper = {
238239
source += '}';
239240
}
240241
else if(group.max < Infinity) {
241-
if(source.length) {
242+
if(we) {
242243
source += ' else '
243244
}
245+
else {
246+
we = true;
247+
}
244248

245249
source += 'if(' + argName + '.length >= ' + group.min + ' && arguments.length <= ' + group.max + ') {';
246250

@@ -256,7 +260,9 @@ var $helper = {
256260
else {
257261
nf = false;
258262

259-
if(source.length) {
263+
if(we) {
264+
we = true;
265+
260266
source += ' else {'
261267

262268
if(group.methods.length === 1) {
@@ -288,7 +294,6 @@ var $helper = {
288294
}
289295
}
290296
}
291-
//console.log(source);
292297

293298
if(/\bType\b/.test(source)) {
294299
variable[name] = eval('(function(Type){return function(' + parameters + ') {' + source + '};})').apply(null, [Type]);
@@ -298,18 +303,18 @@ var $helper = {
298303
}
299304
}, // }}}
300305
methodCheck: function(group, call, argName, refName, returns) { // {{{
301-
var {error, source} = $helper.methodCheckTree(group.methods, 0, call, argName, refName, returns);
306+
var tree = $helper.methodCheckTree(group.methods, 0, call, argName, refName, returns);
302307

303-
if(error) {
308+
if(tree.error) {
304309
if(returns) {
305-
source += 'throw new Error("Wrong type of arguments");';
310+
tree.source += 'throw new Error("Wrong type of arguments");';
306311
}
307312
else {
308-
source += ' else {throw new Error("Wrong type of arguments");}';
313+
tree.source += ' else {throw new Error("Wrong type of arguments");}';
309314
}
310315
}
311316

312-
return source;
317+
return tree.source;
313318
}, // }}}
314319
methodCheckTree: function(methods, index, call, argName, refName, returns) { // {{{
315320
var tree = [];
@@ -381,14 +386,14 @@ var $helper = {
381386
item = usage.tree[0];
382387

383388
if(u + 1 === usages.length) {
384-
if(source.length > 1) {
389+
if(source.length) {
385390
source += 'else {';
386391

387392
ne = false;
388393
}
389394
}
390395
else {
391-
if(source.length > 1) {
396+
if(source.length) {
392397
source += 'else ';
393398
}
394399

@@ -623,9 +628,66 @@ var Type = {
623628
Type.isRegex = Type.isRegExp;
624629

625630
var Helper = {
626-
curry: function(self, bind, args = []) { // {{{
627-
return function(...supplements) {
628-
return self.apply(bind, args.concat(supplements));
631+
class: function(api) { // {{{
632+
var clazz;
633+
634+
if(!!api.$create) {
635+
clazz = api.$create;
636+
delete api.$create;
637+
}
638+
else if(!!api.$extends) {
639+
clazz = function() {
640+
clazz.super.apply(this, arguments);
641+
};
642+
}
643+
else {
644+
clazz = function() {};
645+
}
646+
647+
if(!!api.$extends) {
648+
var zuper = function() {};
649+
zuper.prototype = api.$extends.prototype;
650+
clazz.prototype = new zuper();
651+
clazz.prototype.constructor = clazz;
652+
653+
clazz.super = api.$extends;
654+
655+
for(key in api.$extends) {
656+
if(!clazz[key]) {
657+
clazz[key] = api.$extends[key];
658+
}
659+
}
660+
661+
delete api.$extends;
662+
}
663+
664+
if(!!api.$static) {
665+
for(var key in api.$static) {
666+
clazz[key] = api.$static[key];
667+
}
668+
669+
delete api.$static;
670+
}
671+
672+
if(!!api.$name) {
673+
clazz.displayName = api.$name;
674+
delete api.$name;
675+
676+
if(!!api.$version || api.$version === 0) {
677+
clazz.version = api.$version;
678+
delete api.$version;
679+
}
680+
}
681+
682+
for(var key in api) {
683+
clazz.prototype[key] = api[key];
684+
}
685+
686+
return clazz;
687+
}, // }}}
688+
curry: function(self, bind, args) { // {{{
689+
return function() {
690+
return self.apply(bind, [].concat(args, Array.prototype.slice.call(arguments)));
629691
};
630692
}, // }}}
631693
mapArray: function(array, iterator, condition) { // {{{
@@ -874,7 +936,7 @@ var Helper = {
874936
}
875937
}
876938

877-
$helper.methods(options.sealed, '_cm_' + name, '...args', methods, $curry($call.sealedClass, '__ks_sttc_' + name + '_'), 'args', 'classMethods.' + name);
939+
$helper.methods(options.sealed, '_cm_' + name, '', 'var args = Array.prototype.slice.call(arguments);', methods, $curry($call.sealedClass, '__ks_sttc_' + name + '_'), 'args', 'classMethods.' + name);
878940
}
879941
} // }}}
880942
else { // {{{
@@ -914,7 +976,7 @@ var Helper = {
914976
}
915977
}
916978

917-
$helper.methods(options.class, name, '', reflect.classMethods[name], $curry($call.method, '__ks_sttc_' + name + '_', 'arguments'), 'arguments', 'classMethods.' + name);
979+
$helper.methods(options.class, name, '', '', reflect.classMethods[name], $curry($call.method, '__ks_sttc_' + name + '_', 'arguments'), 'arguments', 'classMethods.' + name);
918980
} // }}}
919981
}, // }}}
920982
newInstanceMethod: function(options) { // {{{
@@ -970,7 +1032,7 @@ var Helper = {
9701032
}
9711033
}
9721034

973-
$helper.methods(options.sealed, '_im_' + name, 'that, ...args', methods, $curry($call.sealedInstance, '__ks_func_' + name + '_'), 'args', 'instanceMethods.' + name);
1035+
$helper.methods(options.sealed, '_im_' + name, 'that', 'var args = Array.prototype.slice.call(arguments, 1, arguments.length);', methods, $curry($call.sealedInstance, '__ks_func_' + name + '_'), 'args', 'instanceMethods.' + name);
9741036
}
9751037
} // }}}
9761038
else { // {{{
@@ -1010,12 +1072,13 @@ var Helper = {
10101072
}
10111073
}
10121074

1013-
$helper.methods(options.class.prototype, name, '', reflect.instanceMethods[name], $curry($call.method, '__ks_func_' + name + '_', 'arguments'), 'arguments', 'instanceMethods.' + name);
1075+
$helper.methods(options.class.prototype, name, '', '', reflect.instanceMethods[name], $curry($call.method, '__ks_func_' + name + '_', 'arguments'), 'arguments', 'instanceMethods.' + name);
10141076
} // }}}
10151077
}, // }}}
1016-
vcurry: function(self, bind, ...args) { // {{{
1017-
return function(...supplements) {
1018-
return self.apply(bind, args.concat(supplements));
1078+
vcurry: function(self, bind) { // {{{
1079+
var args = Array.prototype.slice.call(arguments, 2, arguments.length);
1080+
return function() {
1081+
return self.apply(bind, args.concat(Array.prototype.slice.call(arguments)));
10191082
};
10201083
} // }}}
10211084
};

0 commit comments

Comments
 (0)