Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 230 lines (201 sloc) 7.686 kb
1604420 @Whiteknight +documetation for core/Rosella
authored
1 /*
2 The Rosella namespace is the base namespace of the Rosella library. All
3 Rosella libraries are in their own namespaces nested under the ["Rosella"]
4 namespace.
5
6 This namespace provides a handful of basic functions which are used
7 throughout rosella for some base functionality. These functions can be
8 broken into two general groups: functions for creating new objects, and
9 functions for working with types.
10 */
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
11 namespace Rosella
12 {
dff8cba @Whiteknight Add a new function get_winxed_compiler, because a basic compreg call wil...
authored
13 function get_winxed_compiler()
14 {
15 var c = compreg("winxed");
16 if (c != null)
17 return c;
18 load_language("winxed");
19 c = compreg("winxed");
20 if (c != null)
21 return c;
22 Rosella.Error.invalid(__FUNCTION__, "Cannot get Winxed compiler object");
23 }
24
1604420 @Whiteknight +documetation for core/Rosella
authored
25 /* Allocation / Construction Functions
26 These functions allocate a new object using some kind of proto-
27 or meta-object. This meta-object can be a Class PMC, a PMC which can
28 be used to look up a class (String, String array, NameSpace, Key),
29 or something from the P6object system (P6metaclass, P6protoobject)
30 */
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
31
1604420 @Whiteknight +documetation for core/Rosella
authored
32 // Allocate a fresh new object from the meta-object. Do not manipulate it,
33 // initialize it, or call any methods on it.
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
34 function alloc(var proto)
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
35 {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
36 if (proto == null)
37 Rosella.Error.invalid(__FUNCTION__, "type may not be null");
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
38 var object;
a7c3645 @Whiteknight some cleanups and docs for the action library
authored
39 if (proto instanceof 'P6protoobject')
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
40 object = proto.new();
a7c3645 @Whiteknight some cleanups and docs for the action library
authored
41 else {
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
42 var obj_class = get_type_class(proto);
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops to i...
authored
43 object = new(obj_class);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
44 }
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
45 return object;
46 }
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
47
1604420 @Whiteknight +documetation for core/Rosella
authored
48 // P6-ish initializer routine. Allocate a new object and call the BUILD
49 // method on it, if it is defined.
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
50 function build(var proto, var pos [slurpy], var named [slurpy, named])
51 {
52 var object = alloc(proto);
83bad94 @Whiteknight fix
authored
53 var method = find_named_method(object, "BUILD");
a7c3645 @Whiteknight some cleanups and docs for the action library
authored
54 if (method != null)
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
55 object.*method(pos:[flat], named:[flat,named]);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
56 return object;
57 }
58
1604420 @Whiteknight +documetation for core/Rosella
authored
59 // Construct a new object. Allocate a fresh object of the given type and
60 // call the class constructor. The class constructor is a method with the
61 // same name as the short name of the Class.
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
62 function construct(var proto, var pos [slurpy], var named [slurpy,named])
63 {
64 var object = alloc(proto);
65 var class_obj = get_type_class(proto);
7dda3a3 @Whiteknight add a few more test for rosella build/construct. found and fixed a relat...
authored
66 var method = find_named_method(object, class_obj.name());
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct func...
authored
67 if (method != null)
68 object.*method(pos:[flat], named:[flat,named]);
69 return object;
70 }
71
1604420 @Whiteknight +documetation for core/Rosella
authored
72 /* Type Manipulation Functions
73 These functions take a meta-object (one of the types listed above)
74 and extracts useful information from them in a common way.
75 */
76
77 // Get the fully-qualified string name of the type
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
78 function get_type_name(var type)
79 {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
80 if (type == null)
81 Rosella.Error.invalid(__FUNCTION__, "type may not be null");
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
82 if (type instanceof "String")
83 return type;
84
85 if (type instanceof "P6protoobject")
fdbc05b @Whiteknight some fixes, then I ran into a VERY strange problem involving proxies and...
authored
86 type = type.HOW().get_parrotclass(type).get_namespace();
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
87 else if (type instanceof "P6metaclass")
fdbc05b @Whiteknight some fixes, then I ran into a VERY strange problem involving proxies and...
authored
88 type = type.get_parrotclass(type).get_namespace();
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
89 else if (type instanceof "Class")
90 type = type.get_namespace();
91 else if (type instanceof "Key")
92 ${ get_namespace type, type };
93
94 if (type instanceof "NameSpace")
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
95 return join(";", type.get_name());
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops to i...
authored
96
97 if (does(type, "array"))
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
98 return join(";", type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
99 return string(type);
100 }
101
1604420 @Whiteknight +documetation for core/Rosella
authored
102 // Get the Parrot Class PMC associated with the type.
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
103 function get_type_class(var type)
104 {
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
105 if (type == null)
106 Rosella.Error.invalid(__FUNCTION__, "type may not be null");
107
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
108 if (type instanceof "P6metaclass")
79d24da @Whiteknight Fix the winxed version of the xunit loader so it works with P6protoobjec...
authored
109 return type.get_parrotclass(type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
110 if (type instanceof "P6protoobject")
79d24da @Whiteknight Fix the winxed version of the xunit loader so it works with P6protoobjec...
authored
111 return type.HOW().get_parrotclass(type);
112 if (type instanceof "Class")
113 return type;
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
114
115 if (type instanceof "String")
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
116 type = split(";", type);
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops to i...
authored
117 return get_class(type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
118 }
1604420 @Whiteknight +documetation for core/Rosella
authored
119
86276e9 @Whiteknight rewrite the events system to be more robust and usable
authored
120 // Determine if the given object is an instance of the given type.
f81c66e @Whiteknight Rosella.isa -> isa_type for clarity. Refactor the way errors are communi...
authored
121 function isa_type(var type, var object)
86276e9 @Whiteknight rewrite the events system to be more robust and usable
authored
122 {
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
123 if (type == null)
124 Rosella.Error.invalid(__FUNCTION__, "type may not be null");
125
86276e9 @Whiteknight rewrite the events system to be more robust and usable
authored
126 var class_test = get_type_class(type);
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops to i...
authored
127 return isa(object, class_test);
86276e9 @Whiteknight rewrite the events system to be more robust and usable
authored
128 }
129
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
130 /* Bytecode Management Functions
131 */
132
133 // Initialize the Rosella library, loading in any necessary files.
134 function initialize_rosella(var libs [slurpy])
135 {
136 var core = load_packfile("rosella/core.pbc");
137 init_bytecode(core, "load");
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
138 __load_rosella_libraries(libs);
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
139 }
140
141 // Load a Rosella library, by name
142 function load_rosella_library(var libs [slurpy])
143 {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
144 __load_rosella_libraries(libs);
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
145 }
146
147 // Load an array of rosella libraries
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
148 function __load_rosella_libraries[anon](var libs)
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
149 {
150 for (string lib in libs) {
151 string lib_name = sprintf("rosella/%s.pbc", [lib]);
152 var pf = load_packfile(lib_name);
153 init_bytecode(pf, "load");
154 }
155 }
156
157 // Load in the bytecode library, executing the given trigger, if any.
158 function load_bytecode_file(string file,
159 string trigger [optional], int has_trigger [opt_flag])
160 {
161 var pf = load_packfile(file);
162 if (has_trigger && trigger != "" && trigger != null)
163 init_bytecode(pf, trigger);
164 return pf;
165 }
166
167 // Initialize the bytecode, executing subs with the given trigger
168 function init_bytecode(var pf, string trigger)
169 {
170 if (!pf.is_initialized(trigger)) {
171 pf.mark_initialized(trigger);
71c92fe @Whiteknight subs_by_flag -> subs_by_tag. use load_packfile builtin
authored
172 var subs = pf.subs_by_tag(trigger);
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
173 for (var sub in subs)
174 sub();
175 }
176 }
177
1604420 @Whiteknight +documetation for core/Rosella
authored
178 /* Method-Utility Functions
179 These functions help do things with functions and methods. These are
180 NOT part of the standard API of the Rosella namespace, they only exist
181 to help deal with shortcomings in various parts of the toolchain.
182 These functions will disappear when they are no longer needed
183 */
184
185 // find a method PMC on the given object without throwing an exception on
186 // failure.
187 function find_named_method(var object, string meth_name)
188 {
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
189 if (object == null || meth_name == null)
190 Rosella.Error.invalid(__FUNCTION__, "object and meth_name may not be null");
191
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops to i...
authored
192 if (!can(object, meth_name))
4bd87d0 @Whiteknight use 'can' in find_named_method instead of find_method inside a try/catch...
authored
193 return null;
194 return find_method(object, meth_name);
1604420 @Whiteknight +documetation for core/Rosella
authored
195 }
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
196
2feb00e @Whiteknight add a few small comments
authored
197 // Invoke the given method on the object with the given parameters.
198 // The method parameter can be a method object or a String with the name
199 // of the method to lookup.
6e300fe @Whiteknight Refactor indirect method call logic to an invoke_method. Use this in Eve...
authored
200 function invoke_method(var obj, var method, var p, var n)
201 {
202 if (method instanceof 'String') {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
203 var real_method = find_named_method(obj, method);
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
204 if (real_method == null)
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for the...
authored
205 Rosella.Error.invalid(__FUNCTION__, "Cannot find method '%s'", method);
6e300fe @Whiteknight Refactor indirect method call logic to an invoke_method. Use this in Eve...
authored
206 method = real_method;
207 }
208 return obj.*method(p:[flat], n:[flat,named]);
209 }
210
2feb00e @Whiteknight add a few small comments
authored
211 // Get a unique count number, which is guaranteed to be unique throughout
212 // the Rosella library for the duration of the program.
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
213 function get_unique_count()
214 {
215 int i = 0;
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
216 while(1) {
217 i = i + 1;
218 yield i;
219 }
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
220 }
c5d13cc @Whiteknight Add in a set of lifetime managers. The lifetime managers allow us to reu...
authored
221
222 function get_pmc_keyed_hash()
223 {
224 var h = {};
225 h.set_key_type(3);
226 return h;
227 }
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup.wi...
authored
228 }
229
Something went wrong with that request. Please try again.