Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 222 lines (194 sloc) 7.396 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…
authored
11 namespace Rosella
12 {
dff8cba @Whiteknight Add a new function get_winxed_compiler, because a basic compreg call …
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;
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
22 Rosella.Error.error("Cannot get Winxed compiler object");
dff8cba @Whiteknight Add a new function get_winxed_compiler, because a basic compreg call …
authored
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…
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 f…
authored
34 function alloc(var proto)
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
35 {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
36 if (proto == null)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
37 Rosella.Error.error("type may not be null");
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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…
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…
authored
42 var obj_class = get_type_class(proto);
afd26e9 @Whiteknight Don't need a get_class builtin, winxed already has it (and winxed doe…
authored
43 object = new_pmc(obj_class);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
44 }
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct f…
authored
45 return object;
46 }
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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 f…
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 f…
authored
55 object.*method(pos:[flat], named:[flat,named]);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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 f…
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 re…
authored
66 var method = find_named_method(object, class_obj.name());
c9ab2bc @Whiteknight refactor the Rosella.build function. Add in a new Rosella.construct f…
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…
authored
78 function get_type_name(var type)
79 {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
80 if (type == null)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
81 Rosella.Error.error("type may not be null");
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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 …
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…
authored
87 else if (type instanceof "P6metaclass")
fdbc05b @Whiteknight some fixes, then I ran into a VERY strange problem involving proxies …
authored
88 type = type.get_parrotclass(type).get_namespace();
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
89 else if (type instanceof "Class")
90 type = type.get_namespace();
91 else if (type instanceof "Key")
a01fa2f @Whiteknight Convert most raw pirops to inline/builtins
authored
92 type = get_namespace(type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
93
94 if (type instanceof "NameSpace")
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
95 return join(";", type.get_name());
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops t…
authored
96
97 if (does(type, "array"))
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
98 return join(";", type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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…
authored
103 function get_type_class(var type)
104 {
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
105 if (type == null)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
106 Rosella.Error.error("type may not be null");
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
107
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
108 if (type instanceof "P6metaclass")
79d24da @Whiteknight Fix the winxed version of the xunit loader so it works with P6protoob…
authored
109 return type.get_parrotclass(type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
110 if (type instanceof "P6protoobject")
79d24da @Whiteknight Fix the winxed version of the xunit loader so it works with P6protoob…
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…
authored
114
115 if (type instanceof "String")
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
116 type = split(";", type);
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops t…
authored
117 return get_class(type);
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
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 comm…
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)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
124 Rosella.Error.error("type may not be null");
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
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 t…
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 …
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 …
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
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
148 inline __load_rosella_libraries(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.
bd17c74 @Whiteknight [All] First pass, update all libraries to use the new optional parame…
authored
158 function load_bytecode_file(string file, string trigger = null)
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
159 {
160 var pf = load_packfile(file);
bd17c74 @Whiteknight [All] First pass, update all libraries to use the new optional parame…
authored
161 if (trigger != "" && trigger != null)
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
162 init_bytecode(pf, trigger);
163 return pf;
164 }
165
166 // Initialize the bytecode, executing subs with the given trigger
167 function init_bytecode(var pf, string trigger)
168 {
169 if (!pf.is_initialized(trigger)) {
170 pf.mark_initialized(trigger);
71c92fe @Whiteknight subs_by_flag -> subs_by_tag. use load_packfile builtin
authored
171 var subs = pf.subs_by_tag(trigger);
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
172 for (var sub in subs)
173 sub();
174 }
175 }
176
1604420 @Whiteknight +documetation for core/Rosella
authored
177 /* Method-Utility Functions
178 These functions help do things with functions and methods. These are
179 NOT part of the standard API of the Rosella namespace, they only exist
180 to help deal with shortcomings in various parts of the toolchain.
181 These functions will disappear when they are no longer needed
182 */
183
184 // find a method PMC on the given object without throwing an exception on
185 // failure.
186 function find_named_method(var object, string meth_name)
187 {
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
188 if (object == null || meth_name == null)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
189 Rosella.Error.error("object and meth_name may not be null");
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
190
350ba9e @Whiteknight Start updating the Core library to use inline. Move some common ops t…
authored
191 if (!can(object, meth_name))
4bd87d0 @Whiteknight use 'can' in find_named_method instead of find_method inside a try/ca…
authored
192 return null;
193 return find_method(object, meth_name);
1604420 @Whiteknight +documetation for core/Rosella
authored
194 }
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
195
2feb00e @Whiteknight add a few small comments
authored
196 // Invoke the given method on the object with the given parameters.
197 // The method parameter can be a method object or a String with the name
198 // of the method to lookup.
6e300fe @Whiteknight Refactor indirect method call logic to an invoke_method. Use this in …
authored
199 function invoke_method(var obj, var method, var p, var n)
200 {
201 if (method instanceof 'String') {
8c41b85 @Whiteknight Some cleanups to Rosella.winxed, and add lots more test coverage for …
authored
202 var real_method = find_named_method(obj, method);
9137824 @Whiteknight Cleanups and fixes for Proxy
authored
203 if (real_method == null)
1f6bbf6 @Whiteknight [Core] Use Rosella.Error.error to report errors in Core
authored
204 Rosella.Error.error("Cannot find method '%s'", method);
6e300fe @Whiteknight Refactor indirect method call logic to an invoke_method. Use this in …
authored
205 method = real_method;
206 }
207 return obj.*method(p:[flat], n:[flat,named]);
208 }
209
2feb00e @Whiteknight add a few small comments
authored
210 // Get a unique count number, which is guaranteed to be unique throughout
211 // the Rosella library for the duration of the program.
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
212 function get_unique_count()
213 {
214 int i = 0;
b8a1e7a @Whiteknight Cleanup and rearrange some of the code for Core
authored
215 while(1) {
216 i = i + 1;
217 yield i;
218 }
acf6e7a @Whiteknight fix several tests, remove a few unneeded debug messages
authored
219 }
d4cd396 @Whiteknight add a version of the core library written in winxed. Expand the setup…
authored
220 }
221
Something went wrong with that request. Please try again.