await call is done

This commit is contained in:
Dan Onoshko 2015-01-01 23:23:14 +07:00
parent a8d8da556f
commit 5c9e1fd088
5 changed files with 178 additions and 87 deletions

View File

@ -497,7 +497,7 @@ Future plans
@export each as forEach from "underscore" @export each as forEach from "underscore"
@export "mylib" @export "mylib"
- `async` function modificator - `async` function modificator, status: done
async GET(String url) { async GET(String url) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -534,46 +534,26 @@ Future plans
} }
- `await` operator (only with Promise support) - `await` operator (only with Promise support), status: done
String name = await getNameFromServer(id); String name = await getNameFromServer(id);
console.log(name); console.log(name);
to to
getNameFromServer(id).then(function(_ColaRuntime$$fulfilled, _ColaRuntime$$rejected) { var _ColaRuntime$$arguments = arguments;
_ColaRuntime$$promise(getNameFromServer(id), function(_ColaRuntime$$fulfilled, _ColaRuntime$$rejected) {
arguments = _ColaRuntime$$arguments;
if (_ColaRuntime$$rejected) throw _ColaRuntime$$rejected; if (_ColaRuntime$$rejected) throw _ColaRuntime$$rejected;
String name = __ColaRuntime$$fulfilled; String name = _ColaRuntime$$fulfilled;
console.log(name); console.log(name);
}); }, this);
- static typing - static typing
- `@use` expressions - `@use` expressions
@use strict closure @use strict closure
- `@use await closure` wrapped code will be execute on `DOMContentLoaded`, `main` functions will triggered firstly:
// cola
@use await closure
main(){
alert('loaded!');
}
document.title = "Page";
// js
document.addEventListener('DOMContentLoaded', function(){
alert('loaded!');
}, false);
document.addEventListener('DOMContentLoaded', function(){
document.title = "Page";
}, false);
- interface - interface

View File

@ -125,6 +125,7 @@ Cola.OPERATORS = [ // d - different left and right types of vars, s - same
"&&", // binary - s "&&", // binary - s
"||", // binary - s "||", // binary - s
// ColaScript // ColaScript
"await",
"clone", "clone",
"is", "is",
"isnt", "isnt",
@ -759,6 +760,7 @@ Cola.UNARY_PREFIX = Cola.makePredicate([
"+" "+"
]); ]);
Cola.cUNARY_PREFIX = Cola.makePredicate([ Cola.cUNARY_PREFIX = Cola.makePredicate([
"await",
"clone", "clone",
"typeof", "typeof",
"delete", "delete",

View File

@ -150,10 +150,11 @@ Cola.AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
func = prev_func; func = prev_func;
return true; return true;
} }
if (node instanceof Cola.AST_SymbolRef) { if (node instanceof Cola.AST_SymbolRef) {
var name = node.name; var name = node.name;
var sym = node.scope.find_variable(name); var sym = node.scope.find_variable(name);
if (!sym) { if (!sym || func && name == "arguments") {
var g; var g;
if (globals.has(name)) { if (globals.has(name)) {
g = globals.get(name); g = globals.get(name);

View File

@ -153,34 +153,34 @@ Cola._ColaRuntime$$proto = function _ColaRuntime$$proto(_proto) {
}; };
Cola._ColaRuntime$$proto.i = 15; Cola._ColaRuntime$$proto.i = 15;
Cola._ColaRuntime$$rootImport = function _ColaRuntime$$rootImport(module) { Cola._ColaRuntime$$rootImport = function _ColaRuntime$$rootImport(_module) {
var g = typeof window == "undefined" ? global : window; var g = typeof window == "undefined" ? global : window;
for (var i in module) for (var i in _module)
if (module.hasOwnProperty(i)) g[i] = module[i]; if (_module.hasOwnProperty(i)) g[i] = _module[i];
}; };
Cola._ColaRuntime$$rootImport.i = 16; Cola._ColaRuntime$$rootImport.i = 16;
Cola._ColaRuntime$$commonWrapper = function(modules, cache, ids) { Cola._ColaRuntime$$commonWrapper = function(_modules, _cache, _ids) {
function require(name) { function require(name) {
if (cache[name]) return cache[name].exports; if (_cache[name]) return _cache[name].exports;
if (modules[name]) return call(name); if (_modules[name]) return call(name);
throw new Error('Cannot find module "' + name + '"'); throw new Error('Cannot find module "' + name + '"');
} }
require.cola = true; require.cola = true;
require.cache = cache; require.cache = _cache;
require.modules = modules; require.modules = _modules;
function call(id) { function call(id) {
var module = cache[id] = { var module = _cache[id] = {
exports: {} exports: {}
}; };
modules[id].call(module.exports, function(moduleName) { _modules[id].call(module.exports, function(moduleName) {
var dependencies = ids[moduleName]; var dependencies = _ids[moduleName];
return require(dependencies ? dependencies : moduleName); return require(dependencies ? dependencies : moduleName);
}, module, module.exports); }, module, module.exports);
@ -191,13 +191,19 @@ Cola._ColaRuntime$$commonWrapper = function(modules, cache, ids) {
}; };
Cola._ColaRuntime$$commonWrapper.i = 17; Cola._ColaRuntime$$commonWrapper.i = 17;
Cola._ColaRuntime$$rootExport = function _ColaRuntime$$rootExport(exports, module) { Cola._ColaRuntime$$rootExport = function _ColaRuntime$$rootExport(_exports, _module) {
for (var i in module) for (var i in _module)
if (module.hasOwnProperty(i)) exports[i] = module[i]; if (_module.hasOwnProperty(i)) _exports[i] = _module[i];
}; };
Cola._ColaRuntime$$rootExport.i = 18; Cola._ColaRuntime$$rootExport.i = 18;
Cola._ColaRuntime$$arguments_def = { i : 19 }; Cola._ColaRuntime$$promise = function _ColaRuntime$$promise(_promise, _then, _this) {
if (!_promise.then) throw new Error("Function is not async.");
_promise.then(_then.bind(_this));
};
Cola._ColaRuntime$$promise.i = 19;
Cola._ColaRuntime$$arguments_def = { i : 20 };
Cola._ColaRuntime = Cola._ColaRuntime =
Cola._ColaRuntime$$is + Cola._ColaRuntime$$isnt + Cola._ColaRuntime$$modulo + Cola._ColaRuntime$$isset + Cola._ColaRuntime$$is + Cola._ColaRuntime$$isnt + Cola._ColaRuntime$$modulo + Cola._ColaRuntime$$isset +
@ -205,6 +211,7 @@ Cola._ColaRuntime =
Cola._ColaRuntime$$array_asplice + Cola._ColaRuntime$$func_named_args + Cola._ColaRuntime$$func_set_named_args + Cola._ColaRuntime$$error + Cola._ColaRuntime$$array_asplice + Cola._ColaRuntime$$func_named_args + Cola._ColaRuntime$$func_set_named_args + Cola._ColaRuntime$$error +
Cola._ColaRuntime$$array_negate_access + Cola._ColaRuntime$$array_modulo_access + Cola._ColaRuntime$$updateProperty + Cola._ColaRuntime$$array_negate_access + Cola._ColaRuntime$$array_modulo_access + Cola._ColaRuntime$$updateProperty +
Cola._ColaRuntime$$proto + Cola._ColaRuntime$$rootImport + "(" + Cola._ColaRuntime$$commonWrapper + ")([],{},{});" + Cola._ColaRuntime$$rootExport + Cola._ColaRuntime$$proto + Cola._ColaRuntime$$rootImport + "(" + Cola._ColaRuntime$$commonWrapper + ")([],{},{});" + Cola._ColaRuntime$$rootExport +
Cola._ColaRuntime$$promise +
"var arguments;"; "var arguments;";
Cola.Compressor.StdFuncs = { Cola.Compressor.StdFuncs = {

View File

@ -624,14 +624,62 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
var _ColaRuntime$$ast = Cola.parse(Cola._ColaRuntime, { is_js : true }), var _ColaRuntime$$ast = Cola.parse(Cola._ColaRuntime, { is_js : true }),
_ColaRuntime$$hash = options.std_hash, _ColaRuntime$$hash = options.std_hash,
_this = this, _this = this, deep = 0,
required = [], required_hash = {}, exports = [], required = [], required_hash = {}, exports = [],
awaitData = {
"with" : false,
"level" : null,
"indent" : -1,
"expression" : [],
"origExpression" : [],
"body" : [],
},
tt = new Cola.TreeTransformer(function(node, descend, in_list){ tt = new Cola.TreeTransformer(function(node, descend, in_list){
var newNode, props = {}, parent = this.parent(); var newNode, props = {}, parent = this.parent();
node = node.clone(); node = node.clone();
/*
async GET(String url) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange() {
if (xhr.readyState != 4) return;
if (xhr.status == 200) resolve xhr.response;
reject false;
}
xhr.open("GET", url, true);
xhr.send();
}
to
function GET(url) {
var _ColaRuntime$$arguments = arguments;
return new Promise(function(_ColaRuntime$$resolve, _ColaRuntime$$reject) {
arguments = _ColaRuntime$$arguments;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status == 200) return _ColaRuntime$$resolve(xhr.response);
return _ColaRuntime$$reject(false);
}
xhr.open("GET", url, true);
xhr.send();
}.bind(this));
}
*/
if (node instanceof Cola.AST_Defun && node.mods.indexOf("async") != -1) {
node = Cola.FuncAsync(node);
}
/* /*
main(){ main(){
console.log("hello world"); console.log("hello world");
@ -1036,6 +1084,23 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
}); });
} else } else
/*
await a
to
_ColaRuntime$$fulfilled
*/
if(node instanceof Cola.AST_UnaryPrefix && node.operator == 'await'){
awaitData.with = true;
awaitData.expression.push(node.expression);
node = new Cola.AST_SymbolRef({ name: "_ColaRuntime$$fulfilled" });
awaitData.origExpression.push(node);
} else
/* /*
arr[] = 123 arr[] = 123
@ -1292,46 +1357,6 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
}); });
} else } else
/*
async GET(String url) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange() {
if (xhr.readyState != 4) return;
if (xhr.status == 200) resolve xhr.response;
reject false;
}
xhr.open("GET", url, true);
xhr.send();
}
to
function GET(url) {
var _ColaRuntime$$arguments = arguments;
return new Promise(function(_ColaRuntime$$resolve, _ColaRuntime$$reject) {
arguments = _ColaRuntime$$arguments;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status == 200) return _ColaRuntime$$resolve(xhr.response);
return _ColaRuntime$$reject(false);
}
xhr.open("GET", url, true);
xhr.send();
}.bind(this));
}
*/
if (node instanceof Cola.AST_Defun && node.mods.indexOf("async") != -1) {
node = Cola.FuncAsync(node);
} else
/* /*
func(a, b, name : name, c) func(a, b, name : name, c)
@ -3259,13 +3284,89 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
if (node instanceof Cola.AST_Defun && node.mods && node.mods.indexOf("export") != -1) if (node instanceof Cola.AST_Defun && node.mods && node.mods.indexOf("export") != -1)
exports.push(node.name.name); exports.push(node.name.name);
++deep;
if(node instanceof Array){ if(node instanceof Array){
_this = this; _this = this;
node.forEach(function(nd){ node.forEach(function(nd){
nd._descend(nd, _this); nd._descend(nd, _this);
}) })
} else node._descend(node, this); } else node._descend(node, this);
--deep;
if (awaitData.level !== null && awaitData.level > deep) {
awaitData.level = null;
awaitData.indent = -1;
awaitData.body = [];
}
if (parent instanceof Cola.AST_Scope && awaitData.with) {
_ColaRuntime$$hash[Cola._ColaRuntime$$arguments_def.i] = true;
_ColaRuntime$$hash[Cola._ColaRuntime$$promise.i] = true;
awaitData.level = deep;
awaitData.indent++;
awaitData.with = false;
if (!(node instanceof Array)) node = [node];
awaitData.body[awaitData.indent] = node;
node = [];
node.push(new Cola.AST_Var({
mods : [],
type : "dynamic",
definitions : [new Cola.AST_VarDef({
type : "dynamic",
name : new Cola.AST_SymbolVar({ name: "_ColaRuntime$$arguments" }),
value : new Cola.AST_SymbolRef({ name: "arguments" })
})]
}));
var awaitBody = (awaitData.body[awaitData.indent].unshift(new Cola.AST_SimpleStatement({
body : new Cola.AST_Assign({
left : new Cola.AST_SymbolRef({ name: "arguments" }),
operator : "=",
right : new Cola.AST_SymbolRef({ name: "_ColaRuntime$$arguments" })
})
})), awaitData.body[awaitData.indent]);
awaitData.expression = awaitData.expression.reverse();
awaitData.origExpression = awaitData.origExpression.reverse();
awaitData.expression.forEach(function(expression, i) {
awaitBody = new Cola.AST_SimpleStatement({ body : new Cola.AST_Call({
args : [expression, new Cola.AST_Function({
body : awaitBody instanceof Array
? awaitBody : [awaitBody],
argnames : [
new Cola.AST_SymbolFunarg({ name : "_ColaRuntime$$fulfilled" + i }),
new Cola.AST_SymbolFunarg({ name : "_ColaRuntime$$rejected" + i })
]
}), new Cola.AST_This],
expression : new Cola.AST_SymbolRef({ name: "_ColaRuntime$$promise" })
})});
awaitData.origExpression[i].name += i;
});
node.push(awaitBody);
awaitData.expression = [];
awaitData.origExpression = [];
if (awaitData.level !== null && awaitData.level == deep && awaitData.indent > 0) {
Array.prototype.push
.apply(awaitData.body[awaitData.indent - 1], node);
return false;
}
} else
if (awaitData.level !== null && awaitData.level == deep) {
if (node)
Array.prototype.push
.apply(awaitData.body[awaitData.indent], node instanceof Array
? node : [node]);
return false;
}
return node; return node;
}); });