-- major tests for drawings -- We minimize assumptions about specific pixels, and try to test at the level -- of specific shapes. In particular, no tests of freehand drawings. function test_creating_drawing_saves() App.screen.init{width=120, height=60} Editor_state = edit.initialize_test_state() Editor_state.filename = 'foo' Editor_state.lines = load_array{} Text.redraw_all(Editor_state) edit.draw(Editor_state) -- click on button to create drawing edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1) -- file not immediately saved edit.update(Editor_state, 0.01) check_nil(App.filesystem['foo'], 'early') -- wait until save Current_time = Current_time + 3.1 edit.update(Editor_state, 0) -- filesystem contains drawing and an empty line of text check_eq(App.filesystem['foo'], '```lines\n```\n\n', 'check') end function test_draw_line() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.filename = 'foo' Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'line' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- draw a line edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1) edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 2, '#points') check_eq(drawing.shapes[1].mode, 'line', 'shape:1') local p1 = drawing.points[drawing.shapes[1].p1] local p2 = drawing.points[drawing.shapes[1].p2] check_eq(p1.x, 5, 'p1:x') check_eq(p1.y, 6, 'p1:y') check_eq(p2.x, 35, 'p2:x') check_eq(p2.y, 36, 'p2:y') -- wait until save Current_time = Current_time + 3.1 edit.update(Editor_state, 0) -- The format on disk isn't perfectly stable. Table fields can be reordered. -- So just reload from disk to verify. load_from_disk(Editor_state) Text.redraw_all(Editor_state) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, 'save/#shapes') check_eq(#drawing.points, 2, 'save/#points') check_eq(drawing.shapes[1].mode, 'line', 'save/shape:1') local p1 = drawing.points[drawing.shapes[1].p1] local p2 = drawing.points[drawing.shapes[1].p2] check_eq(p1.x, 5, 'save/p1:x') check_eq(p1.y, 6, 'save/p1:y') check_eq(p2.x, 35, 'save/p2:x') check_eq(p2.y, 36, 'save/p2:y') end function test_draw_horizontal_line() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'manhattan' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- draw a line that is more horizontal than vertical edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1) edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+26, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 2, '#points') check_eq(drawing.shapes[1].mode, 'manhattan', 'shape_mode') local p1 = drawing.points[drawing.shapes[1].p1] local p2 = drawing.points[drawing.shapes[1].p2] check_eq(p1.x, 5, 'p1:x') check_eq(p1.y, 6, 'p1:y') check_eq(p2.x, 35, 'p2:x') check_eq(p2.y, p1.y, 'p2:y') end function test_draw_circle() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'line' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- draw a circle App.mouse_move(Editor_state.left+4, Editor_state.top+Drawing_padding_top+4) -- hover on drawing edit.run_after_keychord(Editor_state, 'C-o') edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) edit.run_after_mouse_release(Editor_state, Editor_state.left+35+30, Editor_state.top+Drawing_padding_top+36, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 1, '#points') check_eq(drawing.shapes[1].mode, 'circle', 'shape_mode') check_eq(drawing.shapes[1].radius, 30, 'radius') local center = drawing.points[drawing.shapes[1].center] check_eq(center.x, 35, 'center:x') check_eq(center.y, 36, 'center:y') end function test_cancel_stroke() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.filename = 'foo' Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'line' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- start drawing a line edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1) -- cancel edit.run_after_keychord(Editor_state, 'escape') edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 0, '#shapes') end function test_keys_do_not_affect_shape_when_mouse_up() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'line' edit.draw(Editor_state) -- hover over drawing and press 'o' without holding mouse App.mouse_move(Editor_state.left+4, Editor_state.top+Drawing_padding_top+4) -- hover on drawing edit.run_after_keychord(Editor_state, 'o') -- no change to drawing mode check_eq(Editor_state.current_drawing_mode, 'line', 'drawing_mode') -- no change to text either because we didn't run the text_input event end function test_draw_circle_mid_stroke() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'line' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- draw a circle App.mouse_move(Editor_state.left+4, Editor_state.top+Drawing_padding_top+4) -- hover on drawing edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) edit.run_after_text_input(Editor_state, 'o') edit.run_after_mouse_release(Editor_state, Editor_state.left+35+30, Editor_state.top+Drawing_padding_top+36, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 1, '#points') check_eq(drawing.shapes[1].mode, 'circle', 'shape_mode') check_eq(drawing.shapes[1].radius, 30, 'radius') local center = drawing.points[drawing.shapes[1].center] check_eq(center.x, 35, 'center:x') check_eq(center.y, 36, 'center:y') end function test_draw_arc() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) Editor_state.current_drawing_mode = 'circle' edit.draw(Editor_state) check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- draw an arc edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) App.mouse_move(Editor_state.left+35+30, Editor_state.top+Drawing_padding_top+36) edit.run_after_text_input(Editor_state, 'a') -- arc mode edit.run_after_mouse_release(Editor_state, Editor_state.left+35+50, Editor_state.top+Drawing_padding_top+36+50, 1) -- 45° local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 1, '#points') check_eq(drawing.shapes[1].mode, 'arc', 'shape_mode') local arc = drawing.shapes[1] check_eq(arc.radius, 30, 'radius') local center = drawing.points[arc.center] check_eq(center.x, 35, 'center:x') check_eq(center.y, 36, 'center:y') check_eq(arc.start_angle, 0, 'start:angle') check_eq(arc.end_angle, math.pi/4, 'end:angle') end function test_draw_polygon() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) edit.draw(Editor_state) check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode') check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- first point edit.run_after_mouse_press(Editor_state, Editor_state.left+5, Editor_state.top+Drawing_padding_top+6, 1) edit.run_after_text_input(Editor_state, 'g') -- polygon mode -- second point App.mouse_move(Editor_state.left+65, Editor_state.top+Drawing_padding_top+36) edit.run_after_text_input(Editor_state, 'p') -- add point -- final point edit.run_after_mouse_release(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+26, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 3, 'vertices') local shape = drawing.shapes[1] check_eq(shape.mode, 'polygon', 'shape_mode') check_eq(#shape.vertices, 3, 'vertices') local p = drawing.points[shape.vertices[1]] check_eq(p.x, 5, 'p1:x') check_eq(p.y, 6, 'p1:y') local p = drawing.points[shape.vertices[2]] check_eq(p.x, 65, 'p2:x') check_eq(p.y, 36, 'p2:y') local p = drawing.points[shape.vertices[3]] check_eq(p.x, 35, 'p3:x') check_eq(p.y, 26, 'p3:y') end function test_draw_rectangle() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) edit.draw(Editor_state) check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode') check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- first point edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) edit.run_after_text_input(Editor_state, 'r') -- rectangle mode -- second point/first edge App.mouse_move(Editor_state.left+42, Editor_state.top+Drawing_padding_top+45) edit.run_after_text_input(Editor_state, 'p') -- override second point/first edge App.mouse_move(Editor_state.left+75, Editor_state.top+Drawing_padding_top+76) edit.run_after_text_input(Editor_state, 'p') -- release (decides 'thickness' of rectangle perpendicular to first edge) edit.run_after_mouse_release(Editor_state, Editor_state.left+15, Editor_state.top+Drawing_padding_top+26, 1) local drawing = Editor_state.lines[1] check_eq(#drawing.shapes, 1, '#shapes') check_eq(#drawing.points, 5, '#points') -- currently includes every point added local shape = drawing.shapes[1] check_eq(shape.mode, 'rectangle', 'shape_mode') check_eq(#shape.vertices, 4, 'vertices') local p = drawing.points[shape.vertices[1]] check_eq(p.x, 35, 'p1:x') check_eq(p.y, 36, 'p1:y') local p = drawing.points[shape.vertices[2]] check_eq(p.x, 75, 'p2:x') check_eq(p.y, 76, 'p2:y') local p = drawing.points[shape.vertices[3]] check_eq(p.x, 70, 'p3:x') check_eq(p.y, 81, 'p3:y') local p = drawing.points[shape.vertices[4]] check_eq(p.x, 30, 'p4:x') check_eq(p.y, 41, 'p4:y') end function test_draw_rectangle_intermediate() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) edit.draw(Editor_state) check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode') check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].starty, Editor_state.top+Drawing_padding_top, 'baseline/y') check_eq(Editor_state.lines[1].h, 128, 'baseline/y') check_eq(#Editor_state.lines[1].shapes, 0, 'baseline/#shapes') -- first point edit.run_after_mouse_press(Editor_state, Editor_state.left+35, Editor_state.top+Drawing_padding_top+36, 1) edit.run_after_text_input(Editor_state, 'r') -- rectangle mode -- second point/first edge App.mouse_move(Editor_state.left+42, Editor_state.top+Drawing_padding_top+45) edit.run_after_text_input(Editor_state, 'p') -- override second point/first edge App.mouse_move(Editor_state.left+75, Editor_state.top+Drawing_padding_top+76) edit.run_after_text_input(Editor_state, 'p') local drawing = Editor_state.lines[1] check_eq(#drawing.points, 3, '#points') -- currently includes every point added local pending = drawing.pending check_eq(pending.mode, 'rectangle', 'shape_mode') check_eq(#pending.vertices, 2, 'vertices') local p = drawing.points[pending.vertices[1]] check_eq(p.x, 35, 'p1:x') check_eq(p.y, 36, 'p1:y') local p = drawing.points[pending.vertices[2]] check_eq(p.x, 75, 'p2:x') check_eq(p.y, 76, 'p2:y') -- outline of rectangle is drawn based on where the mouse is, but we can't check that so far end function test_draw_square() -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) App.screen.init{width=Test_margin_left+256, height=300} -- drawing coordinates 1:1 with pixels Editor_state = edit.initialize_test_state() Editor_state.lines = load_array{'```lines', '```', ''} Text.redraw_all(Editor_state) edit.draw(Editor_state) check_eq(Editor_state.current_drawing_mode, 'line', 'baseline/drawing_mode') check_eq(#Editor_state.lines, 2, 'baseline/#lines') check_eq(Editor_state.lines[1].mode, 'drawing', 'baseline/mode') check_eq(Editor_state.line_cache[1].sta
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This file implements the FFI part of the evaluator for Nimrod code.
import ast, astalgo, ropes, types, options, tables, dynlib, libffi, msgs
when defined(windows):
const libcDll = "msvcrt.dll"
else:
const libcDll = "libc.so(.6|.5|)"
type
TDllCache = tables.TTable[string, TLibHandle]
var
gDllCache = initTable[string, TLibHandle]()
gExeHandle = LoadLib()
proc getDll(cache: var TDllCache; dll: string; info: TLineInfo): pointer =
result = cache[dll]
if result.isNil:
var libs: seq[string] = @[]
libCandidates(dll, libs)
for c in libs:
result = LoadLib(c)
if not result.isNil: break
if result.isNil:
GlobalError(info, "cannot load: " & dll)
cache[dll] = result
const
nkPtrLit = nkIntLit # hopefully we can get rid of this hack soon
proc importcSymbol*(sym: PSym): PNode =
let name = ropeToStr(sym.loc.r)
# the AST does not support untyped pointers directly, so we use an nkIntLit
# that contains the address instead:
result = newNodeIT(nkPtrLit, sym.info, sym.typ)
case name
of "stdin": result.intVal = cast[TAddress](system.stdin)
of "stdout": result.intVal = cast[TAddress](system.stdout)
of "stderr": result.intVal = cast[TAddress](system.stderr)
else:
let lib = sym.annex
if lib != nil and lib.path.kind notin {nkStrLit..nkTripleStrLit}:
GlobalError(sym.info, "dynlib needs to be a string lit for the REPL")
var theAddr: pointer
if lib.isNil and not gExehandle.isNil:
# first try this exe itself:
theAddr = gExehandle.symAddr(name)
# then try libc:
if theAddr.isNil:
let dllhandle = gDllCache.getDll(libcDll, sym.info)
theAddr = dllhandle.checkedSymAddr(name)
else:
let dllhandle = gDllCache.getDll(lib.path.strVal, sym.info)
theAddr = dllhandle.checkedSymAddr(name)
result.intVal = cast[TAddress](theAddr)
proc mapType(