Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadeweka committed Nov 14, 2020
1 parent b2ec0b0 commit 624c312
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 323 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ The term 'anyoli' means 'green' in the Maasai language, thus naming 'anyolite'.

#### Usability

* [ ] Documentation on how to wrap nontrivial functions
* [X] Documentation updates for the new wrapping routines
* [X] Functions with only an operator in their name can now be wrapped using `MrbWrap::Empty`

#### Bugfixes
Expand Down
18 changes: 9 additions & 9 deletions src/MrbCast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module MrbCast

struct_wrapper = MrbMacro.convert_from_ruby_struct(mrb, new_ruby_object, typeof(value))
struct_wrapper.value = MrbWrap::StructWrapper(typeof(value)).new(value)

MrbRefTable.add(MrbRefTable.get_object_id(struct_wrapper.value), ptr.as(Void*))

return new_ruby_object
Expand All @@ -79,7 +79,7 @@ module MrbCast
new_ruby_object = MrbInternal.new_empty_object(mrb, ruby_class, ptr.as(Void*), MrbTypeCache.register(typeof(value), destructor))

MrbMacro.convert_from_ruby_object(mrb, new_ruby_object, typeof(value)).value = value

MrbRefTable.add(MrbRefTable.get_object_id(value), ptr.as(Void*))

return new_ruby_object
Expand Down Expand Up @@ -129,13 +129,13 @@ module MrbCast

def self.interpret_ruby_value(mrb : MrbInternal::MrbState*, value : MrbInternal::MrbValue)
case value.tt
when MrbInternal::MrbVType::MRB_TT_UNDEF then MrbWrap::Undef
when MrbInternal::MrbVType::MRB_TT_TRUE then true
when MrbInternal::MrbVType::MRB_TT_FALSE then (value.value.value_int != 0 ? false : nil) # TODO: Use proper mruby methods
when MrbInternal::MrbVType::MRB_TT_FIXNUM then MrbCast.cast_to_int(mrb, value)
when MrbInternal::MrbVType::MRB_TT_FLOAT then MrbCast.cast_to_float(mrb, value)
when MrbInternal::MrbVType::MRB_TT_STRING then MrbCast.cast_to_string(mrb, value)
else raise("MrbValue #{value} with #{value.tt} is not supported")
when MrbInternal::MrbVType::MRB_TT_UNDEF then MrbWrap::Undef
when MrbInternal::MrbVType::MRB_TT_TRUE then true
when MrbInternal::MrbVType::MRB_TT_FALSE then (value.value.value_int != 0 ? false : nil) # TODO: Use proper mruby methods
when MrbInternal::MrbVType::MRB_TT_FIXNUM then MrbCast.cast_to_int(mrb, value)
when MrbInternal::MrbVType::MRB_TT_FLOAT then MrbCast.cast_to_float(mrb, value)
when MrbInternal::MrbVType::MRB_TT_STRING then MrbCast.cast_to_string(mrb, value)
else raise("MrbValue #{value} with #{value.tt} is not supported")
end
end

Expand Down
8 changes: 4 additions & 4 deletions src/MrbInternal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ lib MrbInternal
rest : MrbValue*
end

fun mrb_open() : MrbState*
fun mrb_open : MrbState*
fun mrb_close(mrb : MrbState*)

fun mrb_define_module(mrb : MrbState*, name : LibC::Char*) : RClass*
Expand All @@ -91,9 +91,9 @@ lib MrbInternal

fun mrb_get_args(mrb : MrbState*, format : LibC::Char*, ...) : MrbInt

fun get_nil_value() : MrbValue
fun get_false_value() : MrbValue
fun get_true_value() : MrbValue
fun get_nil_value : MrbValue
fun get_false_value : MrbValue
fun get_true_value : MrbValue
fun get_fixnum_value(value : MrbInt) : MrbValue
fun get_bool_value(value : MrbBool) : MrbValue
fun get_float_value(mrb : MrbState*, value : MrbFloat) : MrbValue
Expand Down
37 changes: 18 additions & 19 deletions src/MrbMacro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ module MrbMacro
kw_args.num = {{keyword_args.size}}
kw_args.values = Pointer(MrbInternal::MrbValue).malloc(size: {{keyword_args.size}})
kw_args.table = kw_names
kw_args.required = {{keyword_args.values.select {|i| i.class_name != "TupleLiteral"}.size}}
kw_args.required = {{keyword_args.values.select { |i| i.class_name != "TupleLiteral" }.size}}
kw_args.rest = Pointer(MrbInternal::MrbValue).malloc(size: 1)
kw_args
end
Expand Down Expand Up @@ -513,11 +513,11 @@ module MrbMacro
{{mrb_state}}.define_method("initialize", MrbClassCache.get({{crystal_class}}), wrapped_method)
end

macro wrap_method_index(mrb_state, crystal_class, method_index, ruby_name,
is_constructor = false, is_class_method = false,
operator = "", cut_name = nil,
without_keywords = false, added_keyword_args = nil,
context = nil)
macro wrap_method_index(mrb_state, crystal_class, method_index, ruby_name,
is_constructor = false, is_class_method = false,
operator = "", cut_name = nil,
without_keywords = false, added_keyword_args = nil,
context = nil)

{% if is_class_method %}
{% method = crystal_class.resolve.class.methods[method_index] %}
Expand Down Expand Up @@ -619,7 +619,7 @@ module MrbMacro

{% for method in crystal_class.resolve.methods %}
{% all_annotations_specialize_im = crystal_class.resolve.annotations(MrbWrap::SpecializeInstanceMethod) %}
{% annotation_specialize_im = all_annotations_specialize_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_specialize_im = all_annotations_specialize_im.find { |element| element[0].stringify == method.name.stringify } %}

{% if method.annotation(MrbWrap::Specialize) %}
{% has_specialized_method[method.name.stringify] = true %}
Expand All @@ -634,16 +634,16 @@ module MrbMacro

{% for method, index in crystal_class.resolve.methods %}
{% all_annotations_exclude_im = crystal_class.resolve.annotations(MrbWrap::ExcludeInstanceMethod) %}
{% annotation_exclude_im = all_annotations_exclude_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_exclude_im = all_annotations_exclude_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_specialize_im = crystal_class.resolve.annotations(MrbWrap::SpecializeInstanceMethod) %}
{% annotation_specialize_im = all_annotations_specialize_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_specialize_im = all_annotations_specialize_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_rename_im = crystal_class.resolve.annotations(MrbWrap::RenameInstanceMethod) %}
{% annotation_rename_im = all_annotations_rename_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_rename_im = all_annotations_rename_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_without_keywords_im = crystal_class.resolve.annotations(MrbWrap::WrapWithoutKeywordsInstanceMethod) %}
{% annotation_without_keyword_im = all_annotations_without_keywords_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_without_keyword_im = all_annotations_without_keywords_im.find { |element| element[0].stringify == method.name.stringify } %}

{% if method.annotation(MrbWrap::Rename) %}
{% ruby_name = method.annotation(MrbWrap::Rename)[0].id %}
Expand Down Expand Up @@ -724,7 +724,7 @@ module MrbMacro

{% for method in crystal_class.resolve.class.methods %}
{% all_annotations_specialize_im = crystal_class.resolve.annotations(MrbWrap::SpecializeClassMethod) %}
{% annotation_specialize_im = all_annotations_specialize_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_specialize_im = all_annotations_specialize_im.find { |element| element[0].stringify == method.name.stringify } %}

{% if method.annotation(MrbWrap::Specialize) %}
{% has_specialized_method[method.name.stringify] = true %}
Expand All @@ -740,16 +740,16 @@ module MrbMacro
# TODO: Replace all im here with cm
{% for method, index in crystal_class.resolve.class.methods %}
{% all_annotations_exclude_im = crystal_class.resolve.annotations(MrbWrap::ExcludeClassMethod) %}
{% annotation_exclude_im = all_annotations_exclude_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_exclude_im = all_annotations_exclude_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_specialize_im = crystal_class.resolve.annotations(MrbWrap::SpecializeClassMethod) %}
{% annotation_specialize_im = all_annotations_specialize_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_specialize_im = all_annotations_specialize_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_rename_im = crystal_class.resolve.annotations(MrbWrap::RenameClassMethod) %}
{% annotation_rename_im = all_annotations_rename_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_rename_im = all_annotations_rename_im.find { |element| element[0].stringify == method.name.stringify } %}

{% all_annotations_without_keywords_im = crystal_class.resolve.annotations(MrbWrap::WrapWithoutKeywordsClassMethod) %}
{% annotation_without_keyword_im = all_annotations_without_keywords_im.find {|element| element[0].stringify == method.name.stringify} %}
{% annotation_without_keyword_im = all_annotations_without_keywords_im.find { |element| element[0].stringify == method.name.stringify } %}

{% if method.annotation(MrbWrap::Rename) %}
{% ruby_name = method.annotation(MrbWrap::Rename)[0].id %}
Expand Down Expand Up @@ -801,10 +801,10 @@ module MrbMacro
macro wrap_all_constants(mrb_state, crystal_class, exclusions, verbose)
{% for constant, index in crystal_class.resolve.constants %}
{% all_annotations_exclude_im = crystal_class.resolve.annotations(MrbWrap::ExcludeConstant) %}
{% annotation_exclude_im = all_annotations_exclude_im.find {|element| element[0].stringify == constant.stringify} %}
{% annotation_exclude_im = all_annotations_exclude_im.find { |element| element[0].stringify == constant.stringify } %}

{% all_annotations_rename_im = crystal_class.resolve.annotations(MrbWrap::RenameConstant) %}
{% annotation_rename_im = all_annotations_rename_im.find {|element| element[0].stringify == constant.stringify} %}
{% annotation_rename_im = all_annotations_rename_im.find { |element| element[0].stringify == constant.stringify } %}

{% if annotation_rename_im && constant.stringify == annotation_rename_im[0].stringify %}
{% ruby_name = annotation_rename_im[1].id %}
Expand Down Expand Up @@ -835,5 +835,4 @@ module MrbMacro
MrbWrap.wrap_constant_under_class({{mrb_state}}, {{under_class_or_module}}, {{ruby_name}}, {{under_class_or_module}}::{{value}})
{% end %}
end

end
10 changes: 5 additions & 5 deletions src/MrbRefTable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Therefore, the GC won't delete the wrapped objects until necessary.
# Note that this is currently one-directional, so mruby might still delete Crystal objects generated from Crystal itself.
# Furthermore, this is only possible as a module due to C closure limitations.
#
#
# TODO: Add compilation option for ignoring entry checks
module MrbRefTable
@@content = {} of UInt64 => Tuple(Void*, Int64)

@@options = {
:logging => false,
:warnings => true,
:replace_conflicting_pointers => false
:logging => false,
:warnings => true,
:replace_conflicting_pointers => false,
}

def self.get(identification)
Expand All @@ -25,7 +25,7 @@ module MrbRefTable
if option_active?(:replace_conflicting_pointers)
@@content[identification] = {value, @@content[identification][1] + 1}
else
@@content[identification] = {@@content[identification][0] , @@content[identification][1] + 1}
@@content[identification] = {@@content[identification][0], @@content[identification][1] + 1}
end
else
@@content[identification] = {value, @@content[identification][1] + 1}
Expand Down
Loading

0 comments on commit 624c312

Please sign in to comment.