Skip to content

Commit

Permalink
implements SCRoutine
Browse files Browse the repository at this point in the history
- fix bytecode
  - add iter.hasNext
  - add iter.clone
  - fix iter.function$while
  - add Bytecode#state
  - add SCFunction#state
- implements SCRoutine
- implements SCThread
- fix main
- build v0.0.48
  • Loading branch information
mohayonao committed Jun 13, 2014
1 parent ae12f5e commit 6177b6e
Show file tree
Hide file tree
Showing 11 changed files with 978 additions and 635 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.47",
"version": "0.0.48",
"author": "Nao Yonamine <mohayonao@gmail.com>",
"homepage": "http://mohayonao.github.io/SCScript/",
"bugs": "https://github.com/mohayonao/SCScript/issues",
Expand Down
3 changes: 2 additions & 1 deletion src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
STATE_SUSPENDED: 5,
STATE_DONE : 6,

LOOP_BREAK: 0xffff,
STATE_LOOP_BREAK: 10,
STATE_PENDING : 20,
};

if (typeof global.sc !== "undefined") {
Expand Down
4 changes: 4 additions & 0 deletions src/sc/classlib/Core/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ SCScript.install(function(sc) {
);
return this;
}, "body");

spec.state = function() {
return $.Integer(this._.state());
};
});

});
8 changes: 8 additions & 0 deletions src/sc/classlib/Core/Function_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,13 @@
expect(iterator.execute).to.be.calledWith(iter, $body);
expect(test).to.equal(instance);
}));
it("#state", function() {
var instance, test;

instance = this.createInstance();

test = instance.state();
expect(test).to.be.a("SCInteger").that.equals(sc.C.STATE_INIT);
});
});
})();
171 changes: 64 additions & 107 deletions src/sc/classlib/Core/Thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,112 +7,49 @@ SCScript.install(function(sc) {
var fn = sc.lang.fn;
var random = sc.libs.random;

sc.lang.klass.define("Thread", function(spec, utils) {
sc.lang.klass.define("Thread : Stream", function(spec, utils) {

spec.constructor = function SCThread() {
this.__super__("Stream");
};

spec.$new = function($func) {
spec.$new = fn(function($func) {
return this.__super__("new")._init($func);
};

spec._init = function() {
this._state = sc.C.STATE_INIT;
this._randgen = new random.RandGen((Math.random() * 4294967295) >>> 0);
}, "func");

spec._init = function($func) {
if ($func.__tag !== sc.C.TAG_FUNCTION) {
throw new Error("Thread.init failed");
}
this._bytecode = $func._;
this._state = sc.C.STATE_INIT;
this._randgen = new random.RandGen((Math.random() * 4294967295) >>> 0);
return this;
};

spec.state = function() {
return $.Integer(this._state);
};

// spec.parent = function() {
// return this._parent;
// };

// spec.primitiveError = function() {
// return this._primitiveError;
// };

// spec.primitiveIndex = function() {
// return this._primitiveIndex;
// };

// spec.beats = function() {
// return this._beats;
// };

// spec.beats_ = fn(function($inBeats) {
// this._beats = $inBeats;
// this._seconds = this._clock.beats2secs($inBeats);
// return this;
// }, "inBeats");

// spec.seconds = function() {
// return this._seconds;
// };

// spec.seconds_ = fn(function($inSeconds) {
// this._seconds = $inSeconds;
// this._beats = this._clock.secs2beats($inSeconds);
// return this;
// }, "inSeconds");

// spec.clock = function() {
// return this._clock;
// };

// spec.clock_ = fn(function($inClock) {
// this._clock = $inClock;
// this._beats = this._clock.secs2beats(this._seconds);
// return this;
// }, "inClock");

// spec.nextBeat = function() {
// return this._nextBeat;
// };

// spec.endBeat = function() {
// return this._endBeat;
// };

// spec.endBeat_ = function($value) {
// this._endBeat = $value || /* istanbul ignroe next */ $nil;
// return this;
// };

// spec.endValue = function() {
// return this._endValue;
// };

// spec.endValue_ = function($value) {
// this._endValue = $value || /* istanbul ignroe next */ $nil;
// return this;
// };

// spec.exceptionHandler = function() {
// return this._exceptionHandler;
// };

// spec.exceptionHandler_ = function($value) {
// this._exceptionHandler = $value || /* istanbul ignroe next */ $nil;
// return this;
// };

// spec.threadPlayer_ = function($value) {
// this._threadPlayer = $value || /* istanbul ignroe next */ $nil;
// return this;
// };

// spec.executingPath = function() {
// return this._executingPath;
// };

// spec.oldExecutingPath = function() {
// return this._oldExecutingPath;
// };

// TODO: implements parent
// TODO: implements primitiveError
// TODO: implements primitiveIndex
// TODO: implements beats
// TODO: implements beats_
// TODO: implements seconds
// TODO: implements seconds_
// TODO: implements clock
// TODO: implements clock_
// TODO: implements nextBeat
// TODO: implements endBeat
// TODO: implements endBeat_
// TODO: implements endValue
// TODO: implements endValue_
// TODO: implements exceptionHandler
// TODO: implements exceptionHandler_
// TODO: implements threadPlayer_
// TODO: implements executingPath
// TODO: implements oldExecutingPath
// TODO: implements init

spec.copy = utils.nop;
Expand All @@ -121,13 +58,7 @@ SCScript.install(function(sc) {
// return $.Boolean(this._state._ === 5);
// };

// spec.threadPlayer = function() {
// if (this._threadPlayer !== $nil) {
// return this.findThreadPlayer();
// }
// return $nil;
// };

// TODO: implements threadPlayer
// TODO: implements findThreadPlayer

spec.randSeed_ = fn(function($seed) {
Expand Down Expand Up @@ -164,19 +95,45 @@ SCScript.install(function(sc) {
// TODO: implements checkCanArchive
});

sc.lang.klass.define("Routine", function(spec) {
sc.lang.klass.define("Routine : Thread", function(spec, utils) {
var $nil = utils.$nil;

spec.constructor = function SCRoutine() {
this.__super__("Thread");
};

spec.$new = function($func) {
return this.__super__("new", [ $func ]);
};

// TODO: implements $run
// TODO: implements next
// TODO: implements value
// TODO: implements resume
// TODO: implements run
// TODO: implements valueArray
// TODO: implements reset

var routine$resume = function($inval) {
var result;

if (this._state === sc.C.STATE_DONE) {
return $nil;
}

this._state = sc.C.STATE_RUNNING;
result = this._bytecode.resume([ $inval || $nil ]);
this._state = this._bytecode.state();

return result;
};

spec.next = routine$resume;
spec.value = routine$resume;
spec.resume = routine$resume;
spec.run = routine$resume;
spec.valueArray = routine$resume;

spec.reset = function() {
this._state = sc.C.STATE_INIT;
this._bytecode.reset();
return this;
};

// TODO: implements stop
// TODO: implements p
// TODO: implements storeArgs
Expand Down

0 comments on commit 6177b6e

Please sign in to comment.