Skip to content

Commit

Permalink
Merge pull request #22 from jessedoyle/crystal-17-4
Browse files Browse the repository at this point in the history
Version 0.9.0
  • Loading branch information
jessedoyle committed May 27, 2016
2 parents bf9f702 + 3dc466b commit 6ac041e
Show file tree
Hide file tree
Showing 21 changed files with 8,831 additions and 10,418 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# v0.9.0 - May 26, 2016

- Update Duktape to `v1.5.0`. See [release info](https://github.com/svaarala/duktape/blob/master/RELEASES.rst).
- Update to Crystal `0.17.4` syntax.
- Format code using Crystal `0.17.4` formatter.
- Add `NamedTuple` as a type that is allowed as parameter to `call` on a `Duktape::Runtime` instance. NamedTuples will be translated to a hash.
- Optimize for speed (-O2) instead of size (-0s) when building the duktape library.
- Use -Wpedantic as the compiler flag for warnings.

# v0.8.2 - May 5, 2016

- Update to Crystal `0.16.0` syntax.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ version: 1.0.0 # your project's version
dependencies:
duktape:
github: jessedoyle/duktape.cr
version: ~> 0.8.2
version: ~> 0.9.0
```
then execute:
Expand Down Expand Up @@ -140,9 +140,9 @@ Here's an example:
JS
end
rt.call("test", 3, 4, 5) # => 12 (same as test(3, 4, 5);)
rt.call("test", 3, 4, 5) # => 12.0 (same as test(3, 4, 5);)
rt.call(["Math", "PI"]) # => 3.14159
rt.eval("1 + 1") # => 2
rt.eval("1 + 1") # => 2.0
rt.exec("1 + 1") # => nil
```

Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: duktape
version: 0.8.2
version: 0.9.0

authors:
- Jesse Doyle <jdoyle@ualberta.ca>
Expand Down
2 changes: 1 addition & 1 deletion spec/duktape/api/heap_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe Duktape do

describe "create_heap_udata" do
it "should create a heap with a user-argument (Void*)" do
data = "hello, world".to_unsafe as Void*
data = "hello, world".to_unsafe.as(Void*)
heap = Duktape.create_heap_udata(data)
Duktape.destroy_heap heap
end
Expand Down
2 changes: 1 addition & 1 deletion spec/duktape/api/push_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ describe Duktape::API::Push do

describe "push_pointer" do
it "should push a pointer to the stack" do
ptr = "abcd".to_unsafe as Pointer(Void)
ptr = "abcd".to_unsafe.as(Pointer(Void))
ctx.push_pointer ptr

last_stack_type(ctx).should be_js_type(:pointer)
Expand Down
26 changes: 17 additions & 9 deletions spec/duktape/runtime_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ describe Duktape::Runtime do
rt = Duktape::Runtime.new do |sbx|
sbx.eval!("function add(a, b) { return a + b; }")
end
val = rt.call("add", 3.14159_f32, -16_f64) as Float64
val = rt.call("add", 3.14159_f32, -16_f64).as(Float64)

val.should be_a(Float64)
val.to_s.should eq("-12.8584")
val.to_s.should eq("-12.858409881591797")
end

it "should accept arrays as arguments" do
Expand All @@ -119,20 +119,28 @@ describe Duktape::Runtime do

it "should accept hashes as arguments" do
rt = Duktape::Runtime.new
val = rt.call("JSON.stringify", {a: "test", b: 123})
val = rt.call("JSON.stringify", {"a" => "test", "b" => 123})

val.should be_a(String)
val.should eq("{\"a\":\"test\",\"b\":123}")
end

it "should accept nested hashes and arrays as arguments" do
rt = Duktape::Runtime.new
val = rt.call("JSON.stringify", {a: [1, 2, {three: "four"}]})
val = rt.call("JSON.stringify", {"a" => [1, 2, {"three" => "four"}]})

val.should be_a(String)
val.should eq("{\"a\":[1,2,{\"three\":\"four\"}]}")
end

it "should accept NamedTuples as arguments" do
rt = Duktape::Runtime.new
val = rt.call("JSON.stringify", {a: "test", b: 123})

val.should be_a(String)
val.should eq("{\"a\":\"test\",\"b\":123}")
end

it "should return a crystal Array of JSPrimitive" do
rt = Duktape::Runtime.new do |sbx|
sbx.eval!("function same(obj){ return obj; }")
Expand All @@ -147,7 +155,7 @@ describe Duktape::Runtime do
rt = Duktape::Runtime.new do |sbx|
sbx.eval!("function same(obj){ return obj; }")
end
val = rt.call("same", {a: "1", b: "2", c: [3, true]})
val = rt.call("same", {"a" => "1", "b" => "2", "c" => [3, true]})

val.should be_a(Duktape::JSPrimitive)
val.should eq({"a" => "1", "b" => "2", "c" => [3, true]})
Expand All @@ -167,7 +175,7 @@ describe Duktape::Runtime do

it "should call a key without arguments" do
rt = Duktape::Runtime.new
val = rt.call("Math.PI") as Float64
val = rt.call("Math.PI").as(Float64)

val.should_not be_nil
val.floor.should eq(3)
Expand All @@ -183,7 +191,7 @@ describe Duktape::Runtime do

it "should have an empty stack after the call" do
rt = Duktape::Runtime.new
val = rt.call("JSON.stringify", {a: true, b: -10})
val = rt.call("JSON.stringify", {"a" => true, "b" => -10})

val.should be_a(String)
val.should eq("{\"a\":true,\"b\":-10}")
Expand All @@ -194,7 +202,7 @@ describe Duktape::Runtime do
context "with multiple property names" do
it "should call the nested property with arguments" do
rt = Duktape::Runtime.new
val = rt.call(["JSON", "stringify"], 123) as String
val = rt.call(["JSON", "stringify"], 123).as(String)

val.should eq("123")
end
Expand All @@ -219,7 +227,7 @@ describe Duktape::Runtime do

it "should work without any arguments passed" do
rt = Duktape::Runtime.new
val = rt.call(["Math", "E"]) as Float64
val = rt.call(["Math", "E"]).as(Float64)

val.floor.should eq(2)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Duktape.logger.level = Logger::Severity::UNKNOWN
JS_SOURCE_PATH = "#{__DIR__}/javascripts"

REFERENCE_REGEX = /identifier '__abc__' undefined/
SYNTAX_REGEX = /eof or line terminator while parsing string literal/
SYNTAX_REGEX = /eof or line terminator in string litera/
TYPE_REGEX = /undefined not callable/
6 changes: 3 additions & 3 deletions src/duktape/api/buffer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ module Duktape
raise TypeError.new "invalid external buffer"
end

LibDUK.config_buffer ctx, index, buf.to_unsafe as Void*, buf.size
LibDUK.config_buffer ctx, index, buf.to_unsafe.as(Void*), buf.size
end

def get_buffer_data(index : Int32)
ptr = LibDUK.get_buffer_data ctx, index, out size
Slice(UInt8).new ptr as UInt8*, size
Slice(UInt8).new ptr.as(UInt8*), size
end

def push_buffer_object(index : Int32, byte_offset : Int32, byte_length : Int32, flags : UInt32)
Expand All @@ -43,7 +43,7 @@ module Duktape
end

ptr = LibDUK.steal_buffer ctx, index, out size
Slice(UInt8).new ptr as UInt8*, size
Slice(UInt8).new ptr.as(UInt8*), size
end
end
end
6 changes: 3 additions & 3 deletions src/duktape/api/coercion.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module Duktape
def to_buffer(index)
require_valid_index index
flags = LibDUK::BUF_MODE_DONTCARE
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags) as UInt8*
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags).as(UInt8*)
ptr.to_slice size
end

Expand All @@ -41,14 +41,14 @@ module Duktape
def to_dynamic_buffer(index : Int)
require_valid_index index
flags = LibDUK::BUF_MODE_DYNAMIC
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags) as UInt8*
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags).as(UInt8*)
Slice.new ptr, size
end

def to_fixed_buffer(index : Int)
require_valid_index index
flags = LibDUK::BUF_MODE_FIXED
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags) as UInt8*
ptr = LibDUK.to_buffer_raw(ctx, index, out size, flags).as(UInt8*)
Slice.new ptr, size
end

Expand Down
52 changes: 30 additions & 22 deletions src/duktape/api/eval.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ module Duktape
# from errors.
module API::Eval
def eval
flags = LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE
flags = 2 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, nil, 0, flags
end

Expand All @@ -32,10 +33,11 @@ module Duktape
def eval_file(path : String)
validate_file! path

flags = LibDUK::COMPILE_EVAL |
flags = 3 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE

LibDUK.push_string_file_raw ctx, path, 0_u32
LibDUK.push_string_file_raw ctx, path, LibDUK::STRING_PUSH_SAFE
LibDUK.push_string ctx, path
LibDUK.eval_raw ctx, nil, 0, flags
end
Expand All @@ -47,11 +49,12 @@ module Duktape
def eval_file_noresult(path : String)
validate_file! path

flags = LibDUK::COMPILE_EVAL |
flags = 3 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NORESULT

LibDUK.push_string_file_raw ctx, path, 0_u32
LibDUK.push_string_file_raw ctx, path, LibDUK::STRING_PUSH_SAFE
LibDUK.push_string ctx, path
LibDUK.eval_raw ctx, nil, 0, flags
end
Expand All @@ -65,11 +68,12 @@ module Duktape
# methods, so return with an error code.
return LibDUK::ERR_API_ERROR if length < 0

flags = LibDUK::COMPILE_EVAL |
flags = 1 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NOSOURCE
LibDUK::COMPILE_NOSOURCE |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, src, length, flags
end

Expand All @@ -86,12 +90,13 @@ module Duktape
# methods, so return with an error code.
return LibDUK::ERR_API_ERROR if length < 0

flags = LibDUK::COMPILE_EVAL |
flags = 1 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NOSOURCE |
LibDUK::COMPILE_NORESULT
LibDUK::COMPILE_NORESULT |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, src, length, flags
end

Expand All @@ -104,11 +109,12 @@ module Duktape
end

def eval_noresult
flags = LibDUK::COMPILE_EVAL |
flags = 2 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NORESULT
LibDUK::COMPILE_NORESULT |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, nil, 0, flags
end

Expand All @@ -117,12 +123,13 @@ module Duktape
end

def eval_string(src : String)
flags = LibDUK::COMPILE_EVAL |
flags = 1 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_NOSOURCE |
LibDUK::COMPILE_STRLEN |
LibDUK::COMPILE_SAFE
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, src, 0, flags
end

Expand All @@ -131,13 +138,14 @@ module Duktape
end

def eval_string_noresult(src : String)
flags = LibDUK::COMPILE_EVAL |
flags = 1 |
LibDUK::COMPILE_EVAL |
LibDUK::COMPILE_SAFE |
LibDUK::COMPILE_NOSOURCE |
LibDUK::COMPILE_STRLEN |
LibDUK::COMPILE_NORESULT
LibDUK::COMPILE_NORESULT |
LibDUK::COMPILE_NOFILENAME

LibDUK.push_string ctx, __FILE__
LibDUK.eval_raw ctx, src, 0, flags
end

Expand Down
2 changes: 1 addition & 1 deletion src/duktape/api/get.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Duktape

def get_buffer(index : Int32)
require_valid_index index
ptr = LibDUK.get_buffer(ctx, index, out size) as UInt8*
ptr = LibDUK.get_buffer(ctx, index, out size).as(UInt8*)
ptr.to_slice size
end

Expand Down
2 changes: 1 addition & 1 deletion src/duktape/api/push.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Duktape
end

dyn = resizable ? 1_u32 : 0_u32
ptr = LibDUK.push_buffer_raw(ctx, size, dyn) as UInt8*
ptr = LibDUK.push_buffer_raw(ctx, size, dyn).as(UInt8*)
ptr.to_slice size
end

Expand Down
2 changes: 1 addition & 1 deletion src/duktape/api/require.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Duktape
raise TypeError.new "type at #{index} is not buffer"
end

ptr = LibDUK.require_buffer(ctx, index, out size) as UInt8*
ptr = LibDUK.require_buffer(ctx, index, out size).as(UInt8*)
ptr.to_slice size
end

Expand Down
Loading

0 comments on commit 6ac041e

Please sign in to comment.