0
@@ -58,25 +58,24 @@ with the following command:
0
Primitives are normal methods on C++ classes. Comment annotation links the C++
0
method to a symbol with which the primitive is accessed in Ruby code.
0
-For example, consider the Ruby
LookupTable class:
0
+For example, consider the Ruby
Fixnum class:
0
- Ruby.primitive :lookuptable_fetch
0
- raise PrimitiveFailure, "LookupTable#[] primitive failed"
0
+ Ruby.primitive :fixnum_neg
0
+ raise PrimitiveFailure, "Fixnum#-@ primitive failed"
0
-In the C++ file, rbx/vm/builtin_
lookuptable.hpp, the primitive is annotated:
0
+In the C++ file, rbx/vm/builtin_
fixnum.hpp, the primitive is annotated:
0
- class LookupTable : public Object {
0
+ class Fixnum : public Integer {
0
- // Ruby.primitive :lookuptable_fetch
0
- OBJECT fetch(STATE, OBJECT key);
0
+ // Ruby.primitive :fixnum_neg
0
The magic for this happens in rbx/vm/field_extract.rb and the output goes to
0
rbx/vm/gen/primitives_declare.hpp and rbx/vm/gen/primitives_glue.gen.cpp.
0
@@ -86,11 +85,15 @@ rbx/vm/gen/primitives_declare.hpp and rbx/vm/gen/primitives_glue.gen.cpp.
0
--------------------------------------
0
There are two ways to annotate the C++ methods as primitives. If there is a
0
-single C++ method, use 'Ruby.primitive :name_of_primitive'. If there are
0
-multiple C++ methods (i.e. overloaded methods), use 'Ruby.primitive!
0
-:name_of_primitive'. The difference is the '!' method for defining overloaded
0
-methods as primitives. The resulting glue code for overloaded methods looks
0
-something like the following:
0
+single C++ method, use 'Ruby.primitive :name_of_primitive'.
0
+If there are multiple C++ methods (i.e. overloaded methods), use
0
+'Ruby.primitive! :name_of_primitive'. The '!' annotation is used for each
0
+overloaded method and uses the argument types to determine which implementation
0
+method to call for the primitive.
0
+The resulting glue code for overloaded methods looks something like the
0
bool Primitives::float_mul(STATE, VMExecutable* exec,
0
Task* task, Message& msg) {
Comments
No one has commented yet.