public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
Search Repo:
Update with primitives that exist. Clarify bang usage
drbrain (author)
Mon Jul 21 13:29:09 -0700 2008
commit  b7be213064bea00702b90e893e4f25268c97bd70
tree    fccc60f30cd3c6ecfa60cbdc275d0f32852c44b0
parent  917bc9409b82360416197af3ed6a7348b7204b82
...
58
59
60
61
 
62
63
64
65
66
 
 
 
 
67
68
69
70
 
71
72
73
 
 
74
75
76
77
78
79
 
 
80
81
82
...
86
87
88
89
90
91
92
93
 
 
 
 
 
 
 
 
 
94
95
96
...
58
59
60
 
61
62
 
 
 
 
63
64
65
66
67
68
69
 
70
71
72
 
73
74
75
76
 
 
 
 
77
78
79
80
81
...
85
86
87
 
 
 
 
 
88
89
90
91
92
93
94
95
96
97
98
99
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
 
0
-For example, consider the Ruby LookupTable class:
0
+For example, consider the Ruby Fixnum class:
0
 
0
-class LookupTable
0
- def [](key)
0
- Ruby.primitive :lookuptable_fetch
0
- raise PrimitiveFailure, "LookupTable#[] primitive failed"
0
+class Fixnum
0
+ def -@
0
+ Ruby.primitive :fixnum_neg
0
+ raise PrimitiveFailure, "Fixnum#-@ primitive failed"
0
   end
0
 end
0
 
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
 
0
 namespace rubinius {
0
- class LookupTable : public Object {
0
+
0
+ class Fixnum : public Integer {
0
     // ...
0
 
0
- // Ruby.primitive :lookuptable_fetch
0
- OBJECT fetch(STATE, OBJECT key);
0
- }
0
-}
0
+ // Ruby.primitive :fixnum_neg
0
+ INTEGER neg(STATE) {
0
 
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
 
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
+
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
+
0
+The resulting glue code for overloaded methods looks something like the
0
+following:
0
 
0
 bool Primitives::float_mul(STATE, VMExecutable* exec,
0
                            Task* task, Message& msg) {

Comments

    No one has commented yet.