Skip to content

Commit

Permalink
Merge pull request #86 from mohayonao/v0.0.59
Browse files Browse the repository at this point in the history
V0.0.59
  • Loading branch information
mohayonao committed Jun 18, 2014
2 parents a92400a + 135ebbd commit 9dda8b6
Show file tree
Hide file tree
Showing 17 changed files with 332 additions and 117 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scscript",
"version": "0.0.58",
"version": "0.0.59",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
16 changes: 9 additions & 7 deletions src/sc/classlib/Core/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ SCScript.install(function(sc) {

spec.shallowCopy = utils.nop;

var function$run = function(bytecode, args) {
return bytecode.reset().run(args);
};

spec.choose = function() {
return this.value();
};

spec.update = function() {
return this._.reset().run(arguments);
return function$run(this._bytecode, arguments);
};

spec.value = function() {
return this._.reset().run(arguments);
return function$run(this._bytecode, arguments);
};

spec.valueArray = function($args) {
return this._.reset().run($args.asArray()._);
return function$run(this._bytecode, $args.asArray()._);
};

var envir = function(func, args) {
Expand All @@ -60,13 +64,11 @@ SCScript.install(function(sc) {
};

spec.valueEnvir = function() {
var args = envir(this._, arguments);
return this._.reset().run(args);
return function$run(this._bytecode, envir(this._bytecode, arguments));
};

spec.valueArrayEnvir = function($args) {
var args = envir(this._, $args.asArray()._);
return this._.reset().run(args);
return function$run(this._bytecode, envir(this._bytecode, $args.asArray()._));
};

spec.functionPerformList = fn(function($selector, $arglist) {
Expand Down
5 changes: 3 additions & 2 deletions src/sc/classlib/Core/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ SCScript.install(function(sc) {
},
function() {
return $this.reset();
}
},
$.NOP
];
}));
} ];
Expand Down Expand Up @@ -522,7 +523,7 @@ SCScript.install(function(sc) {

spec.yield = function() {
bytecode.yield(this);
return this;
return $nil;
};

// TODO: implements alwaysYield
Expand Down
2 changes: 1 addition & 1 deletion src/sc/classlib/Core/Object_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@
this.stub(bytecode, "yield");

test = instance.yield();
expect(test).to.equal(instance);
expect(test).to.be.a("SCNil");
expect(bytecode.yield).to.be.calledWith(instance);
}));
it.skip("#alwaysYield", function() {
Expand Down
2 changes: 1 addition & 1 deletion src/sc/classlib/Core/Thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SCScript.install(function(sc) {
if ($func.__tag !== sc.TAG_FUNC) {
throw new Error("Thread.init failed");
}
this._bytecode = $func._.setOwner(this);
this._bytecode = $func._bytecode.setOwner(this);
this._state = sc.STATE_INIT;
this._parent = null;
this._randgen = new random.RandGen((Math.random() * 4294967295) >>> 0);
Expand Down
6 changes: 4 additions & 2 deletions src/sc/classlib/Core/Thread_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
},
function() {
return $$(3).yield();
}
},
$.NOP
];
}).value();
},
Expand All @@ -183,7 +184,8 @@
return $$([ 5, 6, 7 ]).do($$(function($_) {
return $_.yield();
}));
}
},
$.NOP
]);

expect(instance.next($inval), 0).to.equal($inval);
Expand Down
6 changes: 4 additions & 2 deletions src/sc/classlib/Streams/Patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ SCScript.install(function(sc) {
counter += 1;
$inval = $outval.yield();
return $inval;
}
},
$.NOP
];
}));

Expand Down Expand Up @@ -279,7 +280,8 @@ SCScript.install(function(sc) {
counter += 1;
$inval = $outval.yield();
return $inval;
}
},
$.NOP
];
}));

Expand Down
32 changes: 18 additions & 14 deletions src/sc/classlib/Streams/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ SCScript.install(function(sc) {
for (i = 0; i < skipSize; ++i) {
$this.next();
}
}
},
$.NOP
];
}).loop();
} ];
Expand Down Expand Up @@ -328,19 +329,22 @@ SCScript.install(function(sc) {
$reset = $false;
return $("Routine").new($.Function(function() {
var $inval;
return [ function(_arg0) {
$inval = _arg0;
if ($reset.__bool__()) {
$this.reset();
$stream.reset();
}
$reset = $true;
$inval = $this.embedInStream($inval);
return $inval;
},
function() {
return $stream.embedInStream($inval);
} ];
return [
function(_arg0) {
$inval = _arg0;
if ($reset.__bool__()) {
$this.reset();
$stream.reset();
}
$reset = $true;
$inval = $this.embedInStream($inval);
return $inval;
},
function() {
return $stream.embedInStream($inval);
},
$.NOP
];
}));
}, "stream");

Expand Down
3 changes: 2 additions & 1 deletion src/sc/classlib/Streams/Stream_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@
},
function() {
return $$(5).yield();
}
},
$.NOP
];
}));

Expand Down
36 changes: 29 additions & 7 deletions src/sc/lang/bytecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
this._argNames = [];
this._argVals = [];
}
if (code.length > 1) {
this._freeFunc = code.pop();
}
this._code = code;
this._length = code.length;
return this.reset();
Expand All @@ -47,6 +50,13 @@
return this;
};

Bytecode.prototype.free = function() {
if (this._freeFunc) {
this._freeFunc();
}
return this;
};

Bytecode.prototype.setOwner = function($owner) {
this._$owner = $owner;
return this;
Expand Down Expand Up @@ -87,12 +97,15 @@
this.state = sc.STATE_RUNNING;

for (i = 0; i < length; ++i) {
result = code[i].apply(this, args);
result = this.update(code[i].apply(this, args));
if (this.state === sc.STATE_BREAK) {
this._iter = null;
break;
}
}
if (this._freeFunc) {
this._freeFunc();
}

bytecode.current = this._parent;
this._parent = null;
Expand Down Expand Up @@ -160,7 +173,7 @@
iter = null;
}

result = code[this._index].apply(this, args);
result = this.update(code[this._index].apply(this, args));

this._index += 1;
if (this._index >= length) {
Expand All @@ -186,12 +199,14 @@
}
if (!this.result) {
this.state = sc.STATE_DONE;
this.free();
}
if (this._parent) {
if (this.state === sc.STATE_DONE) {
this._parent.state = sc.STATE_RUNNING;
} else {
this._parent.state = sc.STATE_SUSPENDED;
this.free();
}
this._parent.purge();
}
Expand All @@ -205,17 +220,24 @@
return this;
};

Bytecode.prototype.push = function($value) {
this._vals.push($value);
return $value;
Bytecode.prototype.push = function() {
this._vals.push(null);
};

Bytecode.prototype.shift = function() {
if (this._vals.length) {
return this._vals.shift();
} else {
return this._parent.shift();
}
return this._parent.shift();
};

Bytecode.prototype.update = function($value) {
if (this._vals.length) {
this._vals[this._vals.length - 1] = $value;
} else if (this._parent) {
this._parent.update($value);
}
return $value;
};

Bytecode.prototype.break = function() {
Expand Down
Loading

0 comments on commit 9dda8b6

Please sign in to comment.