Report inner exceptions in saved stack traces
[jpcrr.git] / luakernel.lua
blob16b56112fc15bddf373b2c24a7bf7d46f68371c8
1 --
2 -- Copyright 2009-2010 Ilari Liusvaara
3 --
4 -- Licenced under GNU GPL v2.
5 --
7 -- Exported tables:
8 -- - args
9 -- Contains user-level arguments.
11 -- Exported functions:
12 -- - All lua standard functions in tables main, "coroutine", "string" and "table".
13 -- - modulus_split(number num, number...)
14 -- Returns quotents and quotent remainders of number divided by given dividers.
15 -- The number of results returned is equal to number of numbers passed. The
16 -- dividers must be in decreasing order and all dividers must be nonzero and
17 -- should be >1.
18 -- - assert(object ret, object err)
19 -- If ret is considered true, return ret. Otherwise raise err as error. Exception
20 -- if err is nil too, then ret is returned anyway.
21 -- - bit.none(number...)
22 -- Returns 48-bit number that has those bits set that are set in none of its
23 -- arguments.
24 -- - bit.any(number...)
25 -- Returns 48-bit number that has those bits set that are set in any of its
26 -- arguments.
27 -- - bit.parity(number...)
28 -- Returns 48-bit number that has those bits set that are set in even number
29 -- of its arguments.
30 -- - bit.any(number...)
31 -- Returns 48-bit number that has those bits set that are set in all of its
32 -- arguments.
33 -- - bit.lshift(number num, number shift)
34 -- Returns 48 rightmost bits of num shifted by shift places left. Going outside
35 -- shift range 0-47 produces unpredicatable results.
36 -- - bit.rshift(number num, number shift)
37 -- Returns 48 rightmost bits of num shifted by shift places right. Going outside
38 -- shift range 0-47 produces unpredicatable results.
39 -- - bit.arshift(number num, number shift)
40 -- Returns 48 rightmost bits of num shifted by shift places right and bit 47
41 -- value copied to all incoming bits. Going outside shift range 0-47 produces
42 -- unpredicatable results.
43 -- - bit.add(number...)
44 -- Returns sum of all passed numbers modulo 2^48.
45 -- - bit.addneg(number...)
46 -- Returns 0 minus sum of all passed numbers modulo 2^48.
47 -- - bit.addalt(number...)
48 -- Returns sum of odd arguments (first is odd) minus sum of even arguments modulo
49 -- 2^48.
50 -- - bit.tohex(number num)
51 -- Returns hexadecimal string representation of number.
52 -- - jpcrr.wait_message()
53 -- Waits for message (string) to come from message queue. Reutrns nil if interrupted.
54 -- - jpcrr.poll_message()
55 -- Checks if there is message from message queue. Reutrns message if there is one,
56 -- nil if none.
57 -- - jpcrr.wait_pc_stop()
58 -- Waits for PC execution to stop (but does not attempt to actually stop it).
59 -- If script is in frame hold, the frame hold is released.
60 -- Returns true if execution got stopped, false if interrupted.
61 -- - jpcrr.pc_running()
62 -- Returns true if PC is running.
63 -- - jpcrr.clock_time()
64 -- Returns current time or nil if no PC.
65 -- - jpcrr.pc_connected()
66 -- Returns true if PC is connected.
67 -- - jpcrr.wait_pc_attach()
68 -- Wait for PC to attach.
69 -- - jpcrr.next_frame()
70 -- Wait for next frame output hold.
71 -- - jpcrr.in_frame_hold()
72 -- Returns true if in frame hold, false otherwise.
73 -- - jpcrr.keypressed(number key)
74 -- Return true if key is pressed, else false.
75 -- - jpcrr.wait_vga()
76 -- Waits for VGA to enter frame hold mode. Frame hold happens once per frame.
77 -- - jpcrr.release_vga()
78 -- Allow VGA to exit frame hold mode. Wait for frame hold first.
79 -- - jpcrr.vga_resolution()
80 -- Return VGA x and y resolutions. -1x-1 or 0x0 is returned if no valid resolution.
81 -- Should only be called during frame hold.
82 -- - jpcrr.hud.left_gap(number flags, number gap)
83 -- Set left gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
84 -- 1 (2) is set, dump to video dump.
85 -- - jpcrr.hud.right_gap(number flags, number gap)
86 -- Set right gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
87 -- 1 (2) is set, dump to video dump.
88 -- - jpcrr.hud.top_gap(number flags, number gap)
89 -- Set top gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
90 -- 1 (2) is set, dump to video dump.
91 -- - jpcrr.hud.bottom_gap(number flags, number gap)
92 -- Set bottom gap for HUD. If flags has bit 0 (1) set, draw on screen, if bit
93 -- 1 (2) is set, dump to video dump.
94 -- - jpcrr.hud.white_solid_box(number flags, number x, number y, number w, number h)
95 -- Draw with solid opaque box.
96 -- - jpcrr.hud.box(number flags, number x, number y, number w, number h, number linethick,
97 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
98 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
99 -- Draw box with specified size, border line thickness, line color and fill color.
100 -- - jpcrr.hud.circle(number flags, number x, number y, number r, number linethick,
101 -- number lineRed, number lineGreen, number lineBlue, number lineAlpha,
102 -- number fillRed, number fillGreen, number fillBlue, number fillAlpha)
103 -- Draw circle with specified size, border line thickness, line color and fill color.
104 -- - jpcrr.hud.bitmap(number flags, number x, number y, string bmap,
105 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
106 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
107 -- Draw bitmap with specified foreground color and background color.
108 -- - jpcrr.hud.bitmap_binary(number flags, number x, number y, string bmap,
109 -- number fgRed, number fgGreen, number fgBlue, number fgAlpha,
110 -- number bgRed, number bgGreen, number bgBlue, number bgAlpha)
111 -- Draw binary bitmap with specified foreground color and background color.
112 -- - jpcrr.joystick_state()
113 -- Returns nil if no joystick. Otherwise returns hold times for all four axis (numeric)
114 -- followed by button states (boolean).
115 -- - jpcrr.keyboard_leds()
116 -- Returns nil if no keyboard, false if LED status is unknown. Otherwise returns three
117 -- booleans, first being state of num lock, second being state of caps lock and third
118 -- being state of scroll lock.
119 -- - jpcrr.mouse_state()
120 -- Returns nil if no mouse. Otherwise returns X, Y and Z axis pending motions (numeric)
121 -- followed by button states (5 booleans).
122 -- - jpcrr.component_encode(table components)
123 -- Return component encoding for specified components.
124 -- - jpcrr.component_decode(string line)
125 -- Return component decoding for specified line, nil/nil if it doesn't encode
126 -- anything, nil/string if parse error occurs (the error is the second return).
127 -- - jpcrr.save_state(string name)
128 -- Savestate into specified file. Returns name used.
129 -- - jpcrr.save_movie(string name)
130 -- Save movie into specified file. Returns name used.
131 -- - jpcrr.load_state_normal(string name)
132 -- Load specified savestate. Returns name used.
133 -- - jpcrr.load_state_preserve_events(string name)
134 -- Load specified savestate, preserving events. Returns name used.
135 -- - jpcrr.load_state_movie(string name)
136 -- Load specified savestate as movie. Returns name used.
137 -- - jpcrr.assemble()
138 -- Open system settings dialog.
139 -- - jpcrr.change_authors()
140 -- Open change authors dialog.
141 -- - jpcrr.exit = function()
142 -- Exit the Lua VM.
143 -- - jpcrr.ram_dump(string name, boolean binary)
144 -- Dump PC memory to specified file. If binary is true, dump is binary, otherwise
145 -- textual hexadecimal dump.
146 -- - jpcrr.write_byte(number addr, number value)
147 -- Write byte to specified physical address.
148 -- - jpcrr.write_word(number addr, number value)
149 -- Write word to specified physical address (little endian).
150 -- - jpcrr.write_dword(number addr, number value)
151 -- Write dword to specified physical address (little endian).
152 -- - jpcrr.read_byte(number addr)
153 -- Return byte from specified physical address.
154 -- - jpcrr.read_word(number addr)
155 -- Return word from specified physical address (little endian).
156 -- - jpcrr.read_dword(number addr)
157 -- Return dword from specified physical address (little endian).
158 -- - jpcrr.timed_trap(number nsecs)
159 -- Set trap after specified number of nanoseconds. Use nil as nsecs to disable.
160 -- - jpcrr.vretrace_start_trap(boolean is_on)
161 -- Set trap on vretrace start on/off.
162 -- - jpcrr.vretrace_end_trap(boolean is_on)
163 -- Set trap on vretrace end on/off.
164 -- - jpcrr.pc_start()
165 -- Start PC execution.
166 -- - jpcrr.pc_stop()
167 -- Stop PC execution.
168 -- - jpcrr.set_pccontrol_pos(number x, number y)
169 -- Set position of PCControl window.
170 -- - jpcrr.set_luaplugin_pos(number x, number y)
171 -- Set position of LuaPlugin window.
172 -- - jpcrr.set_pcmonitor_pos(number x, number y)
173 -- Set position of PCMonitor window.
174 -- - jpcrr.set_pcstartstoptest_pos(number x, number y)
175 -- Set position of PCStartStopTest window.
176 -- - jpcrr.set_virtualkeyboard_pos(number x, number y)
177 -- Set position of VirtualKeyboard window.
178 -- - jpcrr.stringlessthan(String x, String y)
179 -- Return true if x is before y in codepoint lexical order, otherwise false.
180 -- - jpcrr.screenshot(boolean include_hud)
181 -- Take screen shot (Requires monitor). If include_hud is true, include HUD
182 -- (as shown on screen). Note that this should only be called during frame
183 -- hold or results are pretty much undefined.
184 -- - jpcrr.sendevent(string/number...)
185 -- Sends specified event.
187 -- I/O functions have the following conventions. If function returns any real data, the first
188 -- return value returns this data or is nil. Otherwise first return value is true or false.
189 -- If first return value is nil or false, then the second return value gives textual error
190 -- message for failed operation, or is nil if EOF occured before anything was read.
192 -- Unlink, rename and mkdir don't follow this pattern. They just return true/false to signal
193 -- success or failure.
195 -- Specifying nil as name of file results random filename being used (it even works with unlink,
196 -- mkdir, rename and read-only access, but doesn't make any sense there).
198 -- Class: BinaryFile:
199 -- Binary file for RO or RW access. Methods are as follows:
200 -- - name()
201 -- Return name of file.
202 -- - length()
203 -- Return length of file.
204 -- - set_length(number length)
205 -- Truncate file to specified length
206 -- - read(number offset, number length)
207 -- Read up to length bytes from offset.
208 -- - write(number offset, string content)
209 -- Write content to specified offset.
210 -- - close()
211 -- Close the file.
212 -- Class: BinaryInput:
213 -- Binary file for sequential input. Methods are as follows:
214 -- - name()
215 -- Return name of file.
216 -- - four_to_five()
217 -- Return BinaryInput that is four to five decoding of this stream.
218 -- - text()
219 -- Return stream as TextInput.
220 -- - inflate()
221 -- Return BinaryInput that is inflate of this stream.
222 -- - read(number bytes)
223 -- Read up to bytes bytes from file.
224 -- - read()
225 -- Read the entiere file at once.
226 -- - close()
227 -- Close the file.
228 -- Character set for binary files is Latin-1.
230 -- Class: BinaryOutput:
231 -- Binary file for sequential output. Methods are as follows:
232 -- - name()
233 -- Return name of file.
234 -- - four_to_five()
235 -- Return BinaryOutput that writes four to five encoded output to this stream.
236 -- - text()
237 -- Return stream as TextOutput.
238 -- - deflate()
239 -- Return BinaryOutput that writes deflate output to this stream.
240 -- - write(string content)
241 -- Write string to file.
242 -- - close()
243 -- Close the file.
244 -- Character set for binary files is Latin-1.
246 -- Class: TextInput:
247 -- - name()
248 -- Return name of file.
249 -- - read()
250 -- Read line from file.
251 -- - read_component()
252 -- Read next componented line into array.
253 -- - lines()
254 -- Line iterator function.
255 -- - close()
256 -- Close the file.
257 -- Character set for text files is UTF-8.
259 -- Class: TextOutput:
260 -- - name()
261 -- Return name of file.
262 -- - write(string line)
263 -- Write line line to file.
264 -- - write_component(table components)
265 -- Write componented line.
266 -- - close()
267 -- Close the file.
268 -- Character set for text files is UTF-8.
270 -- Class ArchiveIn:
271 -- - member(string name)
272 -- Open substream for member name. The methods are the same as for io.opentextin.
273 -- - member_binary(string name)
274 -- Open binary (four to five) substream for member name. The methods are the same as
275 -- for io.openbinaryin.
276 -- - close()
277 -- Close the file. Any opened substreams are invalidated.
278 -- - member_list()
279 -- Return table listing all members of archive (sorted by name).
280 -- Class ArchiveOut:
281 -- - member(string name)
282 -- Open substream for member name. The methods are the same as for io.opentextout. Note that
283 -- previous member must be closed before next can open.
284 -- - member_binary(string name)
285 -- Open binary (four to five) substream for member name. The methods are the same as
286 -- for io.openbinaryout. Note that previous substream must be closed before next can open.
287 -- - commit()
288 -- Commit the file. No substream may be open. Closes the file.
289 -- - rollback()
290 -- Rollback the file. No substream may be open. Closes the file.
292 -- - io.open(string name, string mode) -> BinaryFile
293 -- Open file named @name in specified mode. The mode can be 'r' (read only) or 'rw' (read and
294 -- write).
295 -- - io.open_read(string name) -> BinaryInput
296 -- Open file named @name as binary input stream.
297 -- - io.open_write(string name) -> BinaryOutput
298 -- Open file named @name as binary input stream.
299 -- - io.open_arch_read(string name) -> ArchiveIn
300 -- Open file named @name as input archive.
301 -- - io.open_arch_write(string name) -> ArchiveOut
302 -- Open file named @name as output archive.
303 -- - io.mkdir(string name)
304 -- Create directory name. Returns name created on success, nil on failure.
305 -- - io.unlink(string name)
306 -- Delete file/directory name. Returns name deleted on success, nil on failure.
307 -- - io.rename(string old, string new)
308 -- Rename file old -> new. Returns old, new on success, nil on failure.
309 -- - io.transform.text()
310 -- Returns function that calls text() method of its parameter.
311 -- - io.transform.four_to_five()
312 -- Returns function that calls four_to_five() method of its parameter.
313 -- - io.transform.deflate()
314 -- Returns function that calls deflate() method of its parameter.
315 -- - io.transform.inflate()
316 -- Returns function that calls inflate() method of its parameter.
317 -- - io.dotransform(object obj, function...)
318 -- Call specified functions on specified object. If any function fails (first argument nil
319 -- or false), call close method on preceeding object. Otherwise call specified functions
320 -- chained left to right and return the result.
321 -- - io.dotransform2(object obj, string err, function...)
322 -- Similar to dotransform except if obj is nil or false, returns obj, err.
328 local handle, err, chunk, indication, k, v;
330 local loadmod = loadmodule;
331 loadmodule = nil;
333 local export_module_in = function(tab, modname, prefix)
334 local fun = loadmod(modname);
335 for k, v in pairs(fun) do
336 tab[(prefix or "") .. k] = v;
340 jpcrr = {};
341 jpcrr.hud = {};
342 bit = {};
343 io = {};
345 export_module_in(jpcrr, "org.jpc.luaextensions.Base");
346 export_module_in(jpcrr, "org.jpc.luaextensions.InputDevices");
347 export_module_in(jpcrr, "org.jpc.luaextensions.ComponentCoding", "component_");
348 export_module_in(bit, "org.jpc.luaextensions.Bitops");
350 -- Few misc functions.
351 assert = function(val, err)
352 if (not val) and err then
353 error(err);
355 return val;
358 modulus_split = function(number, ...)
359 local dividers = {...};
360 local results = {};
361 local rem;
363 for k, v in ipairs(dividers) do
364 rem = number % v;
365 table.insert(results, (number - rem) / v);
366 number = rem;
369 table.insert(results, number);
370 return unpack(results);
373 local getmtable = getmetatable;
374 local toString = tostring;
375 local inject_binary_file;
376 local inject_binary_input;
377 local inject_binary_output;
378 local inject_text_input;
379 local inject_text_output;
380 local inject_archive_input;
381 local inject_archive_output;
383 -- Class member injectors.
384 inject_binary_file = function(obj, name)
385 local _name = name;
386 getmtable(obj).name = function(obj)
387 return _name;
389 getmtable(obj).__index = function(tab, name)
390 local x = getmtable(obj)[name];
391 if x then
392 return x;
394 error("Invalid method " .. name .. " for BinaryFile");
396 return obj;
399 inject_binary_input = function(obj, underlying, name)
400 local _name = name;
401 local old_four_to_five = getmtable(obj).four_to_five;
402 local old_inflate = getmtable(obj).inflate;
403 local old_text = getmtable(obj).text;
404 local old_read = getmtable(obj).read;
405 local old_close = getmtable(obj).close;
406 local underlying_object = underlying;
408 getmtable(obj).name = function(obj)
409 return _name;
411 getmtable(obj).four_to_five = function(obj)
412 local res, err;
413 res, err = old_four_to_five(obj);
414 if not res then
415 return res, err;
417 return inject_binary_input(res, obj, "four-to-five<" .. _name .. ">");
419 getmtable(obj).inflate = function(obj)
420 local res, err;
421 res, err = old_inflate(obj);
422 if not res then
423 return res, err;
425 return inject_binary_input(res, obj, "inflate<" .. _name .. ">");
427 getmtable(obj).text = function(obj)
428 local res, err;
429 res, err = old_text(obj);
430 if not res then
431 return res, err;
433 return inject_text_input(res, obj, "text<" .. _name .. ">");
435 getmtable(obj).read = function(obj, toread)
436 if toread then
437 return old_read(obj, toread);
438 else
439 local res = "";
440 local ret, err;
441 while true do
442 ret, err = old_read(obj, 16384);
443 if not ret then
444 if not err then
445 return res;
447 return nil, err;
449 res = res .. ret;
453 getmtable(obj).close = function(obj)
454 local ret, err, ret2, err2;
455 ret, err = old_close(obj);
456 if underlying_object then ret2, err2 = underlying_object:close(); end
457 if ret and not ret2 then
458 err = err2;
459 ret = ret2;
461 return ret, err;
463 getmtable(obj).__index = function(tab, name)
464 local x = getmtable(obj)[name];
465 if x then
466 return x;
468 error("Invalid method " .. name .. " for BinaryInput");
470 return obj;
473 inject_binary_output = function(obj, underlying, name)
474 local _name = name;
475 local old_four_to_five = getmtable(obj).four_to_five;
476 local old_deflate = getmtable(obj).deflate;
477 local old_text = getmtable(obj).text;
478 local old_close = getmtable(obj).close;
479 local underlying_object = underlying;
481 getmtable(obj).name = function(obj)
482 return _name;
484 getmtable(obj).four_to_five = function(obj)
485 local res, err;
486 res, err = old_four_to_five(obj);
487 if not res then
488 return res, err;
490 return inject_binary_output(res, obj, "four-to-five<" .. _name .. ">");
492 getmtable(obj).deflate = function(obj)
493 local res, err;
494 res, err = old_deflate(obj);
495 if not res then
496 return res, err;
498 return inject_binary_output(res, obj, "deflate<" .. _name .. ">");
500 getmtable(obj).text = function(obj)
501 local res, err;
502 res, err = old_text(obj);
503 if not res then
504 return res, err;
506 return inject_text_output(res, obj, "text<" .. _name .. ">");
508 getmtable(obj).close = function(obj)
509 local ret, err, ret2, err2;
510 ret, err = old_close(obj);
511 if underlying_object then ret2, err2 = underlying_object:close(); end
512 if ret and not ret2 then
513 err = err2;
514 ret = ret2;
516 return ret, err;
518 getmtable(obj).__index = function(tab, name)
519 local x = getmtable(obj)[name];
520 if x then
521 return x;
523 error("Invalid method " .. name .. " for BinaryOutput");
525 return obj;
528 inject_text_input = function(obj, underlying, name)
529 local _name = name;
530 local old_close = getmtable(obj).close;
531 local underlying_object = underlying;
533 getmtable(obj).lines = function(obj)
534 return function(state, prevline)
535 return state:read();
536 end, obj, nil;
538 getmtable(obj).name = function(obj)
539 return _name;
541 getmtable(obj).close = function(obj)
542 local ret, err, ret2, err2;
543 ret, err = old_close(obj);
544 if underlying_object then ret2, err2 = underlying_object:close(); end
545 if ret and not ret2 then
546 err = err2;
547 ret = ret2;
549 return ret, err;
551 getmtable(obj).__index = function(tab, name)
552 local x = getmtable(obj)[name];
553 if x then
554 return x;
556 error("Invalid method " .. name .. " for TextInput");
558 return obj;
561 inject_text_output = function(obj, underlying, name)
562 local _name = name;
563 local old_close = getmtable(obj).close;
564 local underlying_object = underlying;
566 getmtable(obj).name = function(obj)
567 return _name;
569 getmtable(obj).close = function(obj)
570 local ret, err, ret2, err2;
571 ret, err = old_close(obj);
572 if underlying_object then ret2, err2 = underlying_object:close(); end
573 if ret and underlying_object then
574 err = err2;
575 ret = ret2;
577 return ret, err;
579 getmtable(obj).__index = function(tab, name)
580 local x = getmtable(obj)[name];
581 if x then
582 return x;
584 error("Invalid method " .. name .. " for TextOutput");
586 return obj;
589 inject_archive_input = function(obj, name)
590 local _name = name;
591 local old_member = getmtable(obj).member;
592 local old_member_list = getmtable(obj).member_list;
593 getmtable(obj).member = function(obj, member)
594 local res, err;
595 res, err = old_member(obj, member);
596 if not res then
597 return res, err;
599 return inject_binary_input(res, nil, _name .. "[" .. member .. "]");
601 getmtable(obj).name = function(obj)
602 return _name;
604 getmtable(obj).member_list = function(obj)
605 local tab = old_member_list(obj);
606 if tab then table.sort(tab, jpcrr.stringlessthan); end
607 return tab;
609 getmtable(obj).__index = function(tab, name)
610 local x = getmtable(obj)[name];
611 if x then
612 return x;
614 error("Invalid method " .. name .. " for ArchiveInput");
616 return obj;
619 inject_archive_output = function(obj, name)
620 local _name = name;
621 local old_member = getmtable(obj).member;
622 getmtable(obj).member = function(obj, member)
623 local res, err;
624 res, err = old_member(obj, member);
625 if not res then
626 return res, err;
628 return inject_binary_output(res, nil, _name .. "[" .. member .. "]");
630 getmtable(obj).name = function(obj)
631 return _name;
633 getmtable(obj).__index = function(tab, name)
634 local x = getmtable(obj)[name];
635 if x then
636 return x;
638 error("Invalid method " .. name .. " for ArchiveOutput");
640 return obj;
644 -- Redefined print.
646 local rprint = print_console_msg;
647 print_console_msg = nil;
648 print = function(...)
649 local x = "";
650 local y = {...};
651 local i;
652 for i = 1,#y do
653 if i > 1 then
654 x = x .. "\t" .. toString(y[i]);
655 else
656 x = toString(y[i]);
659 rprint(x);
661 print_console_msg = nil;
664 -- I/O routines.
665 local stringfind = string.find;
666 local randname = loadmod("org.jpc.luaextensions.DelayedDelete").random_temp_name;
667 local path = args["luapath"] or ".";
668 local toresourcename = function(resname)
669 if not resname then
670 return randname(path .. "/", "luatemp-");
673 if not stringfind(resname, "[%d%l%u_%-]") then
674 error("Bad resource name (case 1): " .. resname);
676 if stringfind(resname, "^/") then
677 error("Bad resource name (case 2): " .. resname);
679 if stringfind(resname, "%.%.") then
680 error("Bad resource name (case 3): " .. resname);
682 if stringfind(resname, "\\") then
683 error("Bad resource name (case 4): " .. resname);
686 return resname, path .. "/" .. resname;
691 local openbinin = loadmod("org.jpc.luaextensions.BinaryInFile").open;
692 local openbinout = loadmod("org.jpc.luaextensions.BinaryOutFile").open;
693 local openarchin = loadmod("org.jpc.luaextensions.ArchiveIn").open;
694 local openarchout = loadmod("org.jpc.luaextensions.ArchiveOut").open;
695 local openbinary = loadmod("org.jpc.luaextensions.BinaryFile").open;
697 local baseFS = loadmod("org.jpc.luaextensions.BaseFSOps");
698 local mkdir = baseFS.mkdir;
699 local unlink = baseFS.unlink;
700 local rename = baseFS.rename;
702 local getmtable = getmetatable;
704 loadfile = function(_script)
705 local file, file2, err, content;
706 local x, y;
707 x, y = toresourcename(_script);
708 file, err = openbinin(y, "r");
709 if not file then
710 return nil, "Can't open " .. _script .. ": " .. err;
712 file2, err = file:text();
713 if not file2 then
714 return nil, "Can't transform " .. _script .. ": " .. err;
716 content = "";
717 line = file2:read();
718 while line do
719 content = content .. line .. "\n";
720 line = file2:read();
722 file2:close();
723 file:close();
724 return loadstring(content, _script);
727 io.open = function(name, mode)
728 local _name;
729 local res, err;
730 local y;
731 _name, y = toresourcename(name);
732 res, err = openbinary(y, mode);
733 if not res then
734 return res, err;
736 return inject_binary_file(res, _name);
739 io.open_arch_read = function(name)
740 local _name = name;
741 local res, err;
742 local y;
743 _name, y = toresourcename(name);
744 res, err = openarchin(y);
745 if not res then
746 return res, err;
748 return inject_archive_input(res, _name);
751 io.open_arch_write = function(name)
752 local _name = name;
753 local res, err;
754 local y;
755 _name, y = toresourcename(name);
756 res, err = openarchout(y);
757 if not res then
758 return res, err;
760 return inject_archive_output(res, _name);
763 io.open_read = function(name)
764 local _name = name;
765 local res, err;
766 local y;
767 _name, y = toresourcename(name);
768 res, err = openbinin(y);
769 if not res then
770 return res, err;
772 return inject_binary_input(res, nil, _name);
775 io.open_write = function(name)
776 local _name = name;
777 local res, err;
778 local y;
779 _name, y = toresourcename(name);
780 res, err = openbinout(y);
781 if not res then
782 return res, err;
784 return inject_binary_output(res, nil, _name);
787 io.mkdir = function(name)
788 local _name, y;
789 _name, y = toresourcename(name);
790 if mkdir(y) then
791 return _name;
792 else
793 return nil;
797 io.unlink = function(name)
798 local _name, y;
799 _name, y = toresourcename(name);
800 if unlink(y) then
801 return _name;
802 else
803 return nil;
807 io.rename = function(name1, name2)
808 local _name, y;
809 local _name2, y2;
810 _name, y = toresourcename(name1);
811 _name2, y2 = toresourcename(name2);
812 if rename(y, y2) then
813 return _name, _name2;
814 else
815 return nil;
819 io.transform = {};
821 io.transform.text = function()
822 return function(obj)
823 return obj:text();
827 io.transform.four_to_five = function()
828 return function(obj)
829 return obj:four_to_five();
833 io.transform.inflate = function()
834 return function(obj)
835 return obj:inflate();
839 io.transform.deflate = function()
840 return function(obj)
841 return obj:deflate();
845 io.dotransform = function(obj, ...)
846 local todo = {...};
847 local k, v;
848 local obj2, err;
849 for k, v in ipairs(todo) do
850 obj2, err = v(obj);
851 if not obj2 then
852 obj:close();
853 return nil, err;
855 obj = obj2;
857 return obj;
860 io.dotransform2 = function(obj, err, ...)
861 if not obj then
862 return obj, err;
864 return io.dotransform(obj, err, ...);
869 jpcrr.next_frame = function(name)
870 while true do
871 if not jpcrr.pc_connected() then
872 jpcrr.wait_pc_attach();
874 if jpcrr.in_frame_hold() then
875 jpcrr.release_vga();
877 if jpcrr.wait_vga() then
878 return;
884 -- Various stuff built on top of ECI.
885 local invoke = jpcrr.invoke;
886 local invokecall = jpcrr.call;
887 local invokesync = jpcrr.invoke_synchronous;
889 jpcrr.sendevent = function(...)
890 local arguments = {...};
891 local k, v;
892 for k, v in ipairs(arguments) do
893 arguments[k] = toString(v);
895 invokesync("sendevent", arguments);
898 jpcrr.save_state = function(name)
899 local _name, _fname;
900 _name, _fname = toresourcename(name);
901 invokesync("state-save", {_fname});
902 return _name;
905 jpcrr.save_movie = function(name)
906 local _name, _fname;
907 _name, _fname = toresourcename(name);
908 invokesync("movie-save", {_name});
909 return _name;
912 jpcrr.load_state_normal = function(name)
913 local _name, _fname;
914 _name, _fname = toresourcename(name);
915 invokesync("state-load", {_name});
916 return _name;
919 jpcrr.load_state_preserve_events = function(name)
920 local _name, _fname;
921 _name, _fname = toresourcename(name);
922 invokesync("state-load-noevents", {_name});
923 return _name;
926 jpcrr.load_state_movie = function(name)
927 local _name, _fname;
928 _name, _fname = toresourcename(name);
929 invokesync("state-load-movie", {_name});
930 return _name;
933 jpcrr.assemble = function()
934 invokesync("pc-assemble");
937 jpcrr.change_authors = function()
938 invokesync("change-authors");
941 jpcrr.ram_dump = function(name, binary)
942 local _name, _fname;
943 _name, _fname = toresourcename(name);
944 if binary then
945 invokesync("ram-dump-binary", {_name});
946 else
947 invokesync("ram-dump-text", {_name});
949 return _name;
952 jpcrr.hud.left_gap = function(f, g)
953 invoke("hud-left-gap", {toString(f), toString(g)});
956 jpcrr.hud.right_gap = function(f, g)
957 invoke("hud-right-gap", {toString(f), toString(g)});
960 jpcrr.hud.top_gap = function(f, g)
961 invoke("hud-top-gap", {toString(f), toString(g)});
964 jpcrr.hud.bottom_gap = function(f, g)
965 invoke("hud-bottom-gap", {toString(f), toString(g)});
968 jpcrr.hud.white_solid_box = function(f, x, y, w, h)
969 invoke("hud-white-solid-box", {toString(f), toString(x), toString(y), toString(w), toString(h)});
972 jpcrr.hud.box = function(f, x, y, w, h, t, lr, lg, lb, la, fr, fg, fb, fa)
973 invoke("hud-box", {toString(f), toString(x), toString(y), toString(w), toString(h),
974 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
975 toString(fg), tostring(fb), toString(fa)});
978 jpcrr.hud.circle = function(f, x, y, r, t, lr, lg, lb, la, fr, fg, fb, fa)
979 invoke("hud-circle", {toString(f), toString(x), toString(y), toString(r),
980 toString(t), toString(lr), toString(lg), toString(lb), toString(la), toString(fr),
981 toString(fg), tostring(fb), toString(fa)});
984 jpcrr.hud.bitmap = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
985 invoke("hud-bitmap", {toString(f), toString(x), toString(y), bmap, toString(lr),
986 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
987 toString(fa)});
990 jpcrr.hud.bitmap_binary = function(f, x, y, bmap, lr, lg, lb, la, fr, fg, fb, fa)
991 invoke("hud-bitmap-binary", {toString(f), toString(x), toString(y), bmap, toString(lr),
992 toString(lg), toString(lb), toString(la), toString(fr), toString(fg), tostring(fb),
993 toString(fa)});
996 jpcrr.set_pccontrol_pos = function(x, y)
997 invokesync("pccontrol-setwinpos", {toString(x), toString(y)});
1000 jpcrr.set_luaplugin_pos = function(x, y)
1001 invokesync("luaplugin-setwinpos", {toString(x), toString(y)});
1004 jpcrr.set_pcmonitor_pos = function(x, y)
1005 invokesync("pcmonitor-setwinpos", {toString(x), toString(y)});
1008 jpcrr.set_pcstartstoptest_pos = function(x, y)
1009 invokesync("pcstartstoptest-setwinpos", {toString(x), toString(y)});
1012 jpcrr.set_virtualkeyboard_pos = function(x, y)
1013 invokesync("virtualkeyboard-setwinpos", {toString(x), toString(y)});
1016 jpcrr.exit = function()
1017 invokesync("luaplugin-terminate");
1020 jpcrr.pc_start = function()
1021 invoke("pc-start");
1024 jpcrr.pc_stop = function()
1025 invokesync("pc-stop");
1028 jpcrr.screenshot = function(include_hud)
1029 if include_hud then
1030 invoke("screenshot-renderbuffer");
1031 else
1032 invoke("screenshot-vgabuffer");
1037 jpcrr.vretrace_start_trap = function(is_on)
1038 if is_on then
1039 invokesync("trap-vretrace-start-on");
1040 else
1041 invokesync("trap-vretrace-start-off");
1045 jpcrr.vretrace_end_trap = function(is_on)
1046 if is_on then
1047 invokesync("trap-vretrace-end-on");
1048 else
1049 invokesync("trap-vretrace-end-off");
1053 jpcrr.timed_trap = function(nsecs)
1054 if nsecs then
1055 invokesync("trap-timed", {toString(nsecs)});
1056 else
1057 invokesync("trap-timed-disable");
1061 jpcrr.write_byte = function(addr, value)
1062 invokesync("memory-write", {toString(addr), toString(value), "1"});
1065 jpcrr.write_word = function(addr, value)
1066 invokesync("memory-write", {toString(addr), toString(value), "2"});
1069 jpcrr.write_dword = function(addr, value)
1070 invokesync("memory-write", {toString(addr), toString(value), "4"});
1073 jpcrr.read_byte = function(addr)
1074 local t = {toString(addr), "1"};
1075 t = invokecall("memory-read", t);
1076 return (t or {})[1];
1079 jpcrr.read_word = function(addr)
1080 local t = {toString(addr), "2"};
1081 t = invokecall("memory-read", t);
1082 return (t or {})[1];
1085 jpcrr.read_dword = function(addr)
1086 local t = {toString(addr), "4"};
1087 t = invokecall("memory-read", t);
1088 return (t or {})[1];
1091 jpcrr.invoke = nil;
1092 jpcrr.invoke_synchronous = nil;
1093 jpcrr.call = null
1095 -- Dofile.
1096 dofile = function(_script)
1097 local chunk, err, indication
1098 chunk, err = loadfile(_script);
1099 if not chunk then
1100 error("Kernel: Can't load subscript " .. _script .. ": " .. err);
1102 return chunk();
1105 local args2 = args;
1106 args = null;
1107 args = {};
1108 for k, v in pairs(args2) do
1109 if (#k > 2 and string.byte(k, 1) == 120 and string.byte(k, 2) == 45) then
1110 args[string.sub(k, 3)] = v;
1113 jpcrr_raw = null;
1116 chunk = null;
1117 loaded, err = pcall(function()
1118 chunk, err = loadfile(script);
1119 if not chunk then
1120 error(err);
1122 end);
1123 if not loaded then
1124 print("Kernel: Can't load script " .. script .. ": " .. err);
1125 invoke("luaplugin-terminate");
1126 while true do end
1129 script = null;
1130 indication, err = pcall(chunk);
1131 if not indication then
1132 print("Kernel: Unprotected error in script: " .. err);
1133 invoke("luaplugin-terminate");
1134 while true do end