-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "../spec_helper" | ||
require "socket" | ||
require "colorize" | ||
require "json" | ||
|
||
describe StdLib::Task do | ||
it "exists" do | ||
eval_js <<-JS | ||
typeof(window["StdLib::Task"]) | ||
JS | ||
ret = String.new JSC.to_string script_result | ||
ret.should eq "function" | ||
end | ||
|
||
it "works" do | ||
eval_js <<-JS | ||
var a = new MyFileReader("README.md"); | ||
var task = a.read_content_async(); | ||
var message = ""; | ||
while(!(message = task.await())); //uses `task.yield();` if build without -Dpreview_mt | ||
message; | ||
JS | ||
msg = String.new JSC.to_string script_result | ||
msg.should eq File.read("README.md") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Hah? Now you're wondering how to create Javascript classes. I know it. | ||
# Actually, macro `WebExtension.register_class` had been introduced since version 0.3.1 to help you enroll a plain Crystal' class into JavaScriptCore environment and uses it directly in your JS code. | ||
module JSCClass | ||
macro included | ||
INSTANCES = [] of Void* | ||
@@kconstructor : Pointer(Void)? = nil | ||
|
||
def self.last_created_instance | ||
Box({{@type}}).unbox(INSTANCES.last) | ||
end | ||
|
||
def self.constructor | ||
@@kconstructor ||= WebExtension.register_class({{@type}}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
include WebExtension | ||
|
||
alias JSCTypes = JSCFunction | JSCPrimative | JSCObject | JSC::JSValue | ||
|
||
class StdLib::Task < Channel(JSCTypes) | ||
include JSCClass | ||
|
||
def initialize(p) | ||
super 0 | ||
end | ||
|
||
def send(p) | ||
super(p.to_jsc) | ||
true | ||
end | ||
|
||
@[JSCInstanceMethod] | ||
def wait(p) | ||
while true | ||
i, m = Channel.non_blocking_select(self.receive_select_action) | ||
if m.is_a?(JSCTypes) | ||
return m | ||
end | ||
end | ||
end | ||
|
||
@[JSCInstanceMethod] | ||
def await(p) | ||
select | ||
when m = self.receive | ||
return m | ||
else | ||
return nil | ||
end | ||
end | ||
|
||
@[JSCInstanceMethod] | ||
def yield(p) | ||
Fiber.yield | ||
end | ||
end |