Differences From
Artifact [c1b1ca0dcf]:
1 1 (* [ʞ] src/janet-lang.gcd vi:ft=d
2 2 * ~ lexi hale <lexi@hale.su>
3 3 * 🄯 AGPLv3
4 4 * ? implement the godot-janet interface
5 5 *)
6 6
7 -use <janet.h>;
7 +import "type.h";
8 +import "util-gd.h";
9 +use "util-jn.h";
10 +use <janet.h>;
8 11
9 12 class JanetLang is ScriptLanguageExtension {
10 - use <stdio.h>;
11 - use "util.h";
13 + (* use <stdio.h>; *)
12 14
13 15 new {};
16 +
17 + (* var as array[ref Object]: placeholders; *)
14 18
15 19 impl _get_name() -> string {
16 20 gd_string j;
17 21 _t(string).newWithUtf8Chars(&j, "Janet");
18 22 return j;
19 23 };
20 24
................................................................................
87 91 l("for"), l("forv"), l("forever"),
88 92 l("seq"), l("catseq"),
89 93 l("map"), l("mapcat"),
90 94 l("find"),
91 95 l("array"), l("tuple"),
92 96 l("string"), l("buffer"),
93 97 l("table"), l("struct"),
98 + l("print"), l("prin"), l("pp"),
99 + (* gdjn macros *)
100 + l("defm"), l("defm-"),
101 + l("class"), l("defclass"), l("defclass-"),
102 + l("prop"),
94 103 (* et cetera ad nauseam *)
95 104 };
96 105 gd_packedStringArray r = {};
97 106 _t(packedStringArray).empty(&r, nullptr);
98 107 for (size_t i = 0; i < _sz(words); ++i) {
99 108 gdu_array_string_pushPtr(&r, words[i].w, words[i].sz);
100 109 }
................................................................................
118 127 return janscr -> self;
119 128 };
120 129 impl _create_script() -> ref Object {
121 130 auto janscr = gdjn_class_JanetScriptText_new();
122 131 return janscr -> self;
123 132 };
124 133
134 + use {
135 + #define _typeCode(x) gd_ScriptLanguageExtension_LookupResultType_lookupResult##x
136 + };
125 137 impl _get_documentation() -> array[dictionary] {
126 138 gd_array a = {};
127 139 _t(array).ref(&a, &gdjn_ctx -> gd.dox);
128 140 return gdjn_ctx -> gd.dox;
141 + };
142 + impl _lookup_code
143 + ( string code;
144 + string symbol;
145 + string path;
146 + ref Object owner;
147 + ) -> dictionary {
148 + gd_dictionary d;
149 + _t(dictionary).empty(&d, nullptr);
150 + gd_variant v;
151 +
152 + v = gd_variant_of_int(gd_Error_failed);
153 + gdu_setKey(&d, _pstrLit("result"), &v);
154 +
155 + v = gd_variant_of_int(_typeCode(ScriptLocation));
156 + gdu_setKey(&d, _pstrLit("type"), &v);
157 + return d;
129 158 };
130 159
131 160 impl _init() { /* (* "deprecated" but i still have to impl it?? *) */ };
132 161 impl _frame() {};
133 162 impl _thread_enter() { janet_init(); };
134 163 impl _thread_exit() { janet_deinit(); };
135 164 impl _finish() {};
................................................................................
153 182 _t(dictionary).empty(&dict,nullptr);
154 183 return dict;
155 184 };
156 185 };
157 186
158 187 class JanetScript is ScriptExtension {
159 188 var as string: path;
189 + var JanetTable*: env;
190 +
160 191 new {
161 192 _t(string).empty(&me -> path, nullptr);
193 + me -> env = nullptr;
162 194 };
163 195 del {
164 196 _t(string).dtor(&me -> path);
197 + if (me -> env) janet_gcunroot(janet_wrap_table(me -> env));
198 + };
199 +
200 + class BaseInstance is Object {
201 + var as ref JanetScript: script;
202 + del {
203 + gd_refCounted_unreference(me -> script);
204 + printf("del script instance\n");
205 + };
206 + };
207 + class Instance extends JanetScript_BaseInstance {
208 + };
209 + class Placeholder extends JanetScript_BaseInstance {
165 210 };
166 211
167 - impl _create_instance(array[variant] argv, int argc, ref Object owner, bool refCounted, int error) -> ref Object {
168 -
212 + (* impl _create_instance(array[variant] argv, int argc, ref Object owner, bool refCounted, int error) -> ref Object { *)
213 + impl _can_instantiate() -> bool {
214 + if (me -> env == nullptr) return false;
215 + return true; // FIXME
216 + };
217 + impl _instance_create(ref Object obj) -> ref Object {
218 + auto ci = gdjn_class_JanetScript_Instance_new();
219 + printf("made new script instance\n");
220 + ci -> super.script = me;
221 + gd_refCounted_reference(me -> self);
222 + return ci;
223 + };
224 + impl _placeholder_instance_create(ref Object obj) -> ref Object {
225 + auto ci = gdjn_class_JanetScript_Placeholder_new();
226 + printf("made new script placeholder\n");
227 + ci -> super.script = me;
228 + gd_refCounted_reference(me -> self);
229 + return ci;
169 230 };
170 231 impl _get_language() -> ref ScriptLanguage {
171 232 return gdjn_ctx -> gd.janetLang_inst;
172 233 };
173 234 impl _set_path(string path, bool takeOver) {
174 - if (takeOver) {
175 - _t(string).dtor(&me -> path);
176 - me -> path = path;
177 - } else {
178 - _t(string).dtor(&me -> path);
179 - /* _t(string).copy(&me -> path, (void const*[]) {&path}); */
180 - me -> path = gdu_string_dup(&path);
181 - }
235 + _t(string).dtor(&me -> path);
236 + /* _t(string).copy(&me -> path, (void const*[]) {&path}); */
237 + me -> path = gdu_string_dup(&path);
182 238 };
183 239 impl _get_base_script() -> ref Script {
184 240 return nullptr;
185 241 };
186 242 impl _has_static_method(string-name method) -> bool {
187 243 return false;
188 244 };
................................................................................
190 246 impl _get_global_name(string-name id) -> string-name {
191 247 return gdu_sym_null();
192 248 };
193 249 impl _is_abstract() -> bool {
194 250 return false; (* TODO abuse for non-class scripts? *)
195 251 };
196 252 impl _get_instance_base_type() -> string-name {
197 - return _gdu_intern("Object");
253 + return _gdu_intern("ScriptExtension");
198 254 };
199 255 impl _is_valid() -> bool {
200 256 return true;
201 257 };
202 258 impl _get_documentation() -> array[dictionary] {
203 259 gd_array dox;
204 260 _t(array).empty(&dox, nullptr);
205 261 auto empty = gdu_sym_null();
206 262 _t(array).setTyped(&dox,
207 263 GDEXTENSION_VARIANT_TYPE_DICTIONARY, &empty, &empty);
208 264 _t(stringName).dtor(&empty);
209 265 return dox;
210 266 };
267 +
268 + (*
269 + impl _update_exports() {
270 +
271 + };
272 + impl _placeholder_instance_create
273 + (ref Object forObj) -> ref Object {
274 +
275 + };
276 + impl _placeholder_erased
277 + (ref Object ph) {
278 + };
279 + *)
280 +
211 281 };
212 282
213 283 class JanetScriptText extends JanetScript {
214 - var as string: src;
284 + var pstr: src;
215 285 new {
216 - _t(string).empty(&me -> src, nullptr);
286 + me -> src = (pstr){};
217 287 };
218 288 del {
219 - _t(string).dtor(&me -> src);
289 + _drop(me -> src);
220 290 };
221 291
222 292 impl _has_source_code() -> bool { return true; };
223 293 impl _get_source_code() -> string {
224 - auto d = gdu_string_dup(&me -> src);
225 - return d;
294 + return gdu_str_sz(me -> src.v, me -> src.sz);
295 + /* auto d = gdu_string_dup(&me -> src); */
296 + /* return d; */
226 297 };
227 298 impl _set_source_code(string s) {
228 - _t(string).dtor(&me -> src);
229 - _t(string).copy(&me -> src, (void const*[]) {&s});
299 + _drop(me -> src);
300 + me -> src = gdu_string_pdup(&s);
301 + /* printf("janet: set script %d %s\n", (int)me -> src.sz, me -> src.v); */
302 + gdjn_class_JanetScriptText_method__reload(me, false);
303 + /* _t(string).dtor(&me -> src); */
304 + /* _t(string).copy(&me -> src, (void const*[]) {&s}); */
230 305 };
231 306 impl _reload(bool keepState) -> int (* Error *) {
232 - (* TODO *)
233 - return gd_Error_ok;
307 + auto errorCode = gd_Error_ok;
308 + if (me -> super.env)
309 + janet_gcunroot(janet_wrap_table(me -> super.env));
310 + me -> super.env = nullptr;
311 + pstr path = _gdu_string_stackp(&me -> super.path);
312 +
313 + auto cls = jnu_table_extend(gdjn_ctx -> jn.api, 8);
314 + janet_gcroot(janet_wrap_table(cls));
315 + /* printf("janet: doing bytes %d %s\n", (int)me -> src.sz, me -> src.v); */
316 + int e = janet_dobytes(cls,
317 + (uint8_t const*)me -> src.v, me -> src.sz,
318 + path.v, nullptr);
319 + (* we discard the return value; the environment is what
320 + * we're really interested in here *)
321 + /* printf("janet: bytes done, got %d\n",e); */
322 + if (e == 0) {
323 + me -> super.env = cls;
324 + } else {
325 + _err("janet module could not be loaded");
326 + errorCode = gd_Error_errParseError;
327 + janet_gcunroot(janet_wrap_table(cls));
328 + }
329 +
330 + return errorCode;
234 331 };
235 332 };
236 333
237 334 import {
238 335 typedef struct gdjn_janet_image {
239 336 size_t sz;
240 337 uint8_t* buf;