about summary refs log tree commit diff stats
path: root/html/mu-init.subx.html
blob: 4af3f5c0384d3bff6cb9884ba5ff712aadc704cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - mu-init.subx</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffd7; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #ffffd7; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.subxComment { color: #005faf; }
.LineNr { }
.CommentedCode { color: #8a8a8a; }
.SpecialChar { color: #d70000; }
.Constant { color: #008787; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  var lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/main/mu-init.subx'>https://github.com/akkartik/mu/blob/main/mu-init.subx</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="subxComment"># Initialize the minimal runtime for Mu programs.</span>
<span id="L2" class="LineNr"> 2 </span><span class="subxComment">#</span>
<span id="L3" class="LineNr"> 3 </span><span class="subxComment"># See translate for how this file is used.</span>
<span id="L4" class="LineNr"> 4 </span><span class="subxComment">#</span>
<span id="L5" class="LineNr"> 5 </span><span class="subxComment"># Mu programs start at a function called 'main' with this signature:</span>
<span id="L6" class="LineNr"> 6 </span><span class="subxComment">#   fn main screen: (addr screen), keyboard: (addr keyboard), data-disk: (addr disk)</span>
<span id="L7" class="LineNr"> 7 </span><span class="subxComment">#</span>
<span id="L8" class="LineNr"> 8 </span><span class="subxComment"># All tests must pass first (the &quot;power-on unit test&quot;).</span>
<span id="L9" class="LineNr"> 9 </span>
<span id="L10" class="LineNr">10 </span>== code
<span id="L11" class="LineNr">11 </span>
<span id="L12" class="LineNr">12 </span><span class="SpecialChar">Entry</span>:
<span id="L13" class="LineNr">13 </span>  <span class="subxComment"># initialize stack</span>
<span id="L14" class="LineNr">14 </span>  bd/copy-to-ebp 0/imm32
<span id="L15" class="LineNr">15 </span>  <span class="subxComment">#</span>
<span id="L16" class="LineNr">16 </span><span class="CommentedCode">#?   (main 0 0 Primary-bus-secondary-drive)</span>
<span id="L17" class="LineNr">17 </span>  <span class="subxComment"># always first run tests</span>
<span id="L18" class="LineNr">18 </span>  (run-tests)
<span id="L19" class="LineNr">19 </span>  (<a href='104test.subx.html#L17'>num-test-failures</a>)  <span class="subxComment"># =&gt; eax</span>
<span id="L20" class="LineNr">20 </span>  <span class="subxComment"># call main if tests all passed</span>
<span id="L21" class="LineNr">21 </span>  {
<span id="L22" class="LineNr">22 </span>    3d/compare-eax-and 0/imm32
<span id="L23" class="LineNr">23 </span>    75/jump-if-!= <span class="Constant">break</span>/disp8
<span id="L24" class="LineNr">24 </span>    c7 0/subop/copy *Running-tests? 0/imm32/false
<span id="L25" class="LineNr">25 </span>    (<a href='500fake-screen.mu.html#L385'>clear-real-screen</a>)
<span id="L26" class="LineNr">26 </span>    c7 0/subop/copy *<span class="SpecialChar"><a href='103grapheme.subx.html#L231'>Real-screen-cursor-x</a></span> 0/imm32
<span id="L27" class="LineNr">27 </span>    c7 0/subop/copy *<span class="SpecialChar"><a href='103grapheme.subx.html#L233'>Real-screen-cursor-y</a></span> 0/imm32
<span id="L28" class="LineNr">28 </span>    (main 0 0 <span class="SpecialChar"><a href='boot.subx.html#L948'>Primary-bus-secondary-drive</a></span>)
<span id="L29" class="LineNr">29 </span>  }
<span id="L30" class="LineNr">30 </span>
<span id="L31" class="LineNr">31 </span>  <span class="subxComment"># hang indefinitely</span>
<span id="L32" class="LineNr">32 </span>  {
<span id="L33" class="LineNr">33 </span>    eb/jump <span class="Constant">loop</span>/disp8
<span id="L34" class="LineNr">34 </span>  }
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
s="p">) end function App.run_tests() local sorted_names = {} for name,binding in pairs(_G) do if name:find('test_') == 1 then table.insert(sorted_names, name) end end table.sort(sorted_names) for _,name in ipairs(sorted_names) do App.initialize_for_test() --? print('=== '..name) --? _G[name]() xpcall(_G[name], function(err) prepend_debug_info_to_test_failure(name, err) end) end -- clean up all test methods for _,name in ipairs(sorted_names) do _G[name] = nil end end function App.initialize_for_test() App.screen.init{width=100, height=50} App.screen.contents = {} -- clear screen App.filesystem = {} App.source_dir = '' App.current_dir = '' App.save_dir = '' App.fake_keys_pressed = {} App.fake_mouse_state = {x=-1, y=-1} App.initialize_globals() end -- App.screen.resize and App.screen.move seem like better names than -- love.window.setMode and love.window.setPosition respectively. They'll -- be side-effect-free during tests, and they'll save their results in -- attributes of App.screen for easy access. App.screen={} -- Use App.screen.init in tests to initialize the fake screen. function App.screen.init(dims) App.screen.width = dims.width App.screen.height = dims.height end function App.screen.resize(width, height, flags) App.screen.width = width App.screen.height = height App.screen.flags = flags end function App.screen.size() return App.screen.width, App.screen.height, App.screen.flags end function App.screen.move(x,y, displayindex) App.screen.x = x App.screen.y = y App.screen.displayindex = displayindex end function App.screen.position() return App.screen.x, App.screen.y, App.screen.displayindex end -- If you use App.screen.print instead of love.graphics.print, -- tests will be able to check what was printed using App.screen.check below. -- -- One drawback of this approach: the y coordinate used depends on font size, -- which feels brittle. function App.screen.print(msg, x,y) local screen_row = 'y'..tostring(y) --? print('drawing "'..msg..'" at y '..tostring(y)) local screen = App.screen if screen.contents[screen_row] == nil then screen.contents[screen_row] = {} for i=0,screen.width-1 do screen.contents[screen_row][i] = '' end end if x < screen.width then screen.contents[screen_row][x] = msg end end function App.screen.check(y, expected_contents, msg) --? print('checking for "'..expected_contents..'" at y '..tostring(y)) local screen_row = 'y'..tostring(y) local contents = '' if App.screen.contents[screen_row] == nil then error('no text at y '..tostring(y)) end for i,s in ipairs(App.screen.contents[screen_row]) do contents = contents..s end check_eq(contents, expected_contents, msg) end -- If you access the time using App.get_time instead of love.timer.getTime, -- tests will be able to move the time back and forwards as needed using -- App.wait_fake_time below. App.time = 1 function App.get_time() return App.time end function App.wait_fake_time(t) App.time = App.time + t end function App.width(text) return love.graphics.getFont():getWidth(text) end -- If you access the clipboard using App.get_clipboard and App.set_clipboard -- instead of love.system.getClipboardText and love.system.setClipboardText -- respectively, tests will be able to manipulate the clipboard by -- reading/writing App.clipboard. App.clipboard = '' function App.get_clipboard() return App.clipboard end function App.set_clipboard(s) App.clipboard = s end -- In tests I mostly send chords all at once to the keyboard handlers. -- However, you'll occasionally need to check if a key is down outside a handler. -- If you use App.key_down instead of love.keyboard.isDown, tests will be able to -- simulate keypresses using App.fake_key_press and App.fake_key_release -- below. This isn't very realistic, though, and it's up to tests to -- orchestrate key presses that correspond to the handlers they invoke. App.fake_keys_pressed = {} function App.key_down(key) return App.fake_keys_pressed[key] end function App.fake_key_press(key) App.fake_keys_pressed[key] = true end function App.fake_key_release(key) App.fake_keys_pressed[key] = nil end -- Tests mostly will invoke mouse handlers directly. However, you'll -- occasionally need to check if a mouse button is down outside a handler. -- If you use App.mouse_down instead of love.mouse.isDown, tests will be able to -- simulate mouse clicks using App.fake_mouse_press and App.fake_mouse_release -- below. This isn't very realistic, though, and it's up to tests to -- orchestrate presses that correspond to the handlers they invoke. App.fake_mouse_state = {x=-1, y=-1} -- x,y always set function App.mouse_move(x,y) App.fake_mouse_state.x = x App.fake_mouse_state.y = y end function App.mouse_down(mouse_button) return App.fake_mouse_state[mouse_button] end function App.mouse_x() return App.fake_mouse_state.x end function App.mouse_y() return App.fake_mouse_state.y end function App.fake_mouse_press(x,y, mouse_button) App.fake_mouse_state.x = x App.fake_mouse_state.y = y App.fake_mouse_state[mouse_button] = true end function App.fake_mouse_release(x,y, mouse_button) App.fake_mouse_state.x = x App.fake_mouse_state.y = y App.fake_mouse_state[mouse_button] = nil end -- If you use App.open_for_reading and App.open_for_writing instead of other -- various Lua and LÖVE helpers, tests will be able to check the results of -- file operations inside the App.filesystem table. function App.open_for_reading(filename) if App.filesystem[filename] then return { lines = function(self) return App.filesystem[filename]:gmatch('[^\n]+') end, read = function(self) return App.filesystem[filename] end, close = function(self) end, } end end function App.read_file(filename) return App.filesystem[filename] end function App.open_for_writing(filename) App.filesystem[filename] = '' return { write = function(self, s) App.filesystem[filename] = App.filesystem[filename]..s end, close = function(self) end, } end function App.write_file(filename, contents) App.filesystem[filename] = contents return --[[status]] true end function App.mkdir(dirname) -- nothing in test mode end function App.remove(filename) App.filesystem[filename] = nil end -- Some helpers to trigger an event and then refresh the screen. Akin to one -- iteration of the event loop. -- all textinput events are also keypresses -- TODO: handle chords of multiple keys function App.run_after_textinput(t) App.keypressed(t) App.textinput(t) App.keyreleased(t) App.screen.contents = {} App.draw() end -- not all keys are textinput -- TODO: handle chords of multiple keys function App.run_after_keychord(chord, key) App.keychord_press(chord, key) App.keyreleased(key) App.screen.contents = {} App.draw() end function App.run_after_mouse_click(x,y, mouse_button) App.fake_mouse_press(x,y, mouse_button) App.mousepressed(x,y, mouse_button) App.fake_mouse_release(x,y, mouse_button) App.mousereleased(x,y, mouse_button) App.screen.contents = {} App.draw() end function App.run_after_mouse_press(x,y, mouse_button) App.fake_mouse_press(x,y, mouse_button) App.mousepressed(x,y, mouse_button) App.screen.contents = {} App.draw() end function App.run_after_mouse_release(x,y, mouse_button) App.fake_mouse_release(x,y, mouse_button) App.mousereleased(x,y, mouse_button) App.screen.contents = {} App.draw() end -- miscellaneous internal helpers function App.color(color) love.graphics.setColor(color.r, color.g, color.b, color.a) end -- prepend file/line/test function prepend_debug_info_to_test_failure(test_name, err) local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '') local stack_trace = debug.traceback('', --[[stack frame]]5) local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '') local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number --? local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number..'\t\t'..stack_trace:gsub('\n', '\n\t\t') table.insert(Test_errors, full_error) end nativefs = require 'nativefs' local Keys_down = {} -- call this once all tests are run -- can't run any tests after this function App.disable_tests() -- have LÖVE delegate all handlers to App if they exist -- make sure to late-bind handlers like LÖVE's defaults do for name in pairs(love.handlers) do if App[name] then -- love.keyboard.isDown doesn't work on Android, so emulate it using -- keypressed and keyreleased events if name == 'keypressed' then love.handlers[name] = function(key, scancode, isrepeat) Keys_down[key] = true return App.keypressed(key, scancode, isrepeat) end elseif name == 'keyreleased' then love.handlers[name] = function(key, scancode) Keys_down[key] = nil return App.keyreleased(key, scancode) end else love.handlers[name] = function(...) App[name](...) end end end end -- test methods are disallowed outside tests App.run_tests = nil App.disable_tests = nil App.screen.init = nil App.filesystem = nil App.time = nil App.run_after_textinput = nil App.run_after_keychord = nil App.keypress = nil App.keyrelease = nil App.run_after_mouse_click = nil App.run_after_mouse_press = nil App.run_after_mouse_release = nil App.fake_keys_pressed = nil App.fake_key_press = nil App.fake_key_release = nil App.fake_mouse_state = nil App.fake_mouse_press = nil App.fake_mouse_release = nil -- other methods dispatch to real hardware App.screen.resize = love.window.setMode App.screen.size = love.window.getMode App.screen.move = love.window.setPosition App.screen.position = love.window.getPosition App.screen.print = love.graphics.print App.open_for_reading = function(filename) local result = nativefs.newFile(filename) local ok, err = result:open('r') if ok then return result else return ok, err end end App.read_file = function(path) if not is_absolute_path(path) then return --[[status]] false, 'Please use an unambiguous absolute path.' end local f, err = App.open_for_reading(path) if err then return --[[status]] false, err end local contents = f:read() f:close() return contents end App.open_for_writing = function(filename) local result = nativefs.newFile(filename) local ok, err = result:open('w') if ok then return result else return ok, err end end App.write_file = function(path, contents) if not is_absolute_path(path) then return --[[status]] false, 'Please use an unambiguous absolute path.' end local f, err = App.open_for_writing(path) if err then return --[[status]] false, err end f:write(contents) f:close() return --[[status]] true end App.files = nativefs.getDirectoryItems App.file_info = nativefs.getInfo App.mkdir = nativefs.createDirectory App.remove = nativefs.remove App.source_dir = love.filesystem.getSource()..'/' -- '/' should work even on Windows App.current_dir = nativefs.getWorkingDirectory()..'/' App.save_dir = love.filesystem.getSaveDirectory()..'/' App.get_time = love.timer.getTime App.get_clipboard = love.system.getClipboardText App.set_clipboard = love.system.setClipboardText App.key_down = function(key) return Keys_down[key] end App.mouse_move = love.mouse.setPosition App.mouse_down = love.mouse.isDown App.mouse_x = love.mouse.getX App.mouse_y = love.mouse.getY end