selectors: a.b sends with self, a::b without; drop a:b
The `.` and `::` selectors swap meaning and `a:b` is removed, so every access is one of: a.b read the member a.b(args) call the member WITH a as self a::b static read (no self) a::b(args) call the member WITHOUT self - compiler: postfix `.` emits CALLMETHOD (with self) when followed by `(`, else GETATTR; `::` emits GETATTR + CALL (no self); the `:` form is gone (with a helpful error suggesting `.`/`::`). - `:` remains only for type hints (local x : int), unchanged. - ffi: `lib.func(...)` (with self) and `ffi::load(...)` (no self); the native loaders no longer skip a receiver. - file: `File::open` is static, so cm_open reads args[0]/args[1]. - lexer: fix cat_lexer_kind_name, whose table had drifted from the enum (missing `nil`), mislabelling tokens. - self-hosted parser (std/lang/parser.cat): `.name(` -> method (self), `::name(` -> call (no self); compiler/checker unchanged. - tools/migrate_selectors.py: one-pass migration of the call forms. - regenerated std/glfw and std/vulkan; migrated examples/tests/std and the README; new tests/selectors.cat (130 green). Verified live: GLFW window + Vulkan device still work; `a:b` now errors.
This commit is contained in:
1 parent
04009c3c5e
commit
6f6f444beb
35 files changed
+1114
-964
No files matched your search
@@ -104,25 +104,35 @@ as concretas compartilham seu conjunto de métodos (`read`/`write`/
|
||||
```lua
|
||||
local s = StringIO("hello") -- stream de texto em memória
|
||||
local b = BytesIO() -- stream de bytes em memória
|
||||
b:write("bytes"); print(b:read())
|
||||
b.write("bytes"); print(b.read())
|
||||
|
||||
local f = File::open("x.txt") -- classmethod -> File
|
||||
f:write("data"); f:close()
|
||||
f.write("data"); f.close()
|
||||
|
||||
local r = File::open("x.txt", "r")
|
||||
print(r:read()); r:close()
|
||||
print(r.read()); r.close()
|
||||
```
|
||||
|
||||
`File::open(path[, mode])` é um **classmethod** (sintaxe `::`) que
|
||||
retorna um `File`. `AbstractFile` não é instanciável.
|
||||
`File::open(path[, mode])` é um **classmethod** (sintaxe `::`, **sem**
|
||||
`self`) que retorna um `File`. `AbstractFile` não é instanciável.
|
||||
|
||||
## Class methods (`::`)
|
||||
## Seletores: `.` (com self) e `::` (sem self)
|
||||
|
||||
Chamadas com `::` passam o receptor como primeiro argumento (`self`),
|
||||
como `Object::method(args)` em outras linguagens:
|
||||
`.`, `::` e `[]` são os acessos. A regra:
|
||||
|
||||
```lua
|
||||
local f = File::open("a.txt") -- File::open
|
||||
obj.field -- lê o membro
|
||||
obj.method(args) -- chama o membro COM obj como self
|
||||
obj::static(args) -- chama o membro SEM self
|
||||
```
|
||||
|
||||
Um nome de membro pode ser keyword (`glfw.false`). A antiga sintaxe
|
||||
`obj:method(args)` foi **removida**.
|
||||
|
||||
```lua
|
||||
local f = File::open("a.txt") -- sem self
|
||||
local util = { twice = function(x) return x * 2 end }
|
||||
print(util::twice(21)) -- 42
|
||||
```
|
||||
|
||||
## Type hints avançados
|
||||
@@ -247,7 +257,7 @@ class Dog extends Animal
|
||||
end
|
||||
|
||||
local d = Dog("Rex") -- constrói via `new`
|
||||
print(d:speak()) -- Rex barks
|
||||
print(d.speak()) -- Rex barks
|
||||
```
|
||||
|
||||
## `ffi` — chamar código nativo (estilo ctypes/CDLL)
|
||||
@@ -258,16 +268,16 @@ uma assinatura declarada, usando **libffi** para a chamada ABI-correta
|
||||
|
||||
```lua
|
||||
local m = ffi::load("libm.so.6")
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
local pow = m.func("pow", "double(double,double)")
|
||||
print(pow(2, 10)) -- 1024
|
||||
|
||||
local c = ffi::load("libc.so.6")
|
||||
local strlen = c:func("strlen", "long(string)")
|
||||
local strlen = c.func("strlen", "long(string)")
|
||||
```
|
||||
|
||||
`ffi` é um módulo (namespace): `::` acessa o membro estático
|
||||
(`ffi::load`), enquanto `:` envia mensagem a uma instância
|
||||
(`m:func(...)`).
|
||||
(`ffi::load`, **sem** self), enquanto `.` envia mensagem a uma instância
|
||||
(`m.func(...)`, **com** o `m` como self).
|
||||
|
||||
Gramática da assinatura: `RET(ARG,ARG,...)`, com tipos `void`, `bool`,
|
||||
`char`, `int8/uint8`, `int16/uint16`, `int/long/int32/uint32`,
|
||||
|
||||
@@ -19,13 +19,13 @@ class Dog extends Animal
|
||||
end
|
||||
|
||||
local a = Animal("cat")
|
||||
print(a:speak())
|
||||
print(a:get_name())
|
||||
print(a.speak())
|
||||
print(a.get_name())
|
||||
|
||||
-- subclassing: Dog inherits init/get_name, overrides speak
|
||||
local d = Dog("Rex")
|
||||
print(d:speak())
|
||||
print(d:get_name())
|
||||
print(d.speak())
|
||||
print(d.get_name())
|
||||
|
||||
-- use: import another catscript file (its definitions become global here)
|
||||
use "std/math/math.cat"
|
||||
|
||||
+5
-5
@@ -4,19 +4,19 @@
|
||||
-- signature, and call it. libffi performs the ABI-correct call.
|
||||
|
||||
local m = ffi::load("libm.so.6")
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
local pow = m.func("pow", "double(double,double)")
|
||||
print("pow(2,10) =", pow(2, 10))
|
||||
|
||||
local sqrt = m:func("sqrt", "double(double)")
|
||||
local sqrt = m.func("sqrt", "double(double)")
|
||||
print("sqrt(144) =", sqrt(144))
|
||||
|
||||
local c = ffi::load("libc.so.6")
|
||||
local getpid = c:func("getpid", "int()")
|
||||
local getpid = c.func("getpid", "int()")
|
||||
local pid = getpid()
|
||||
print("getpid > 0:", pid > 0)
|
||||
|
||||
local strlen = c:func("strlen", "long(string)")
|
||||
local strlen = c.func("strlen", "long(string)")
|
||||
print("strlen:", strlen("catscript"))
|
||||
|
||||
local abs = c:func("abs", "int(int)")
|
||||
local abs = c.func("abs", "int(int)")
|
||||
print("abs(-42) =", abs(-42))
|
||||
+8
-8
@@ -6,23 +6,23 @@
|
||||
|
||||
-- in-memory text stream
|
||||
local s = StringIO("hello, catscript")
|
||||
print("stringio:", s:read())
|
||||
print("stringio:", s.read())
|
||||
|
||||
-- in-memory byte stream, written then read back
|
||||
local b = BytesIO()
|
||||
b:write("bytes ")
|
||||
b:write("io")
|
||||
print("bytesio:", b:read())
|
||||
b.write("bytes ")
|
||||
b.write("io")
|
||||
print("bytesio:", b.read())
|
||||
|
||||
-- a real file, opened with the File::open classmethod
|
||||
local path = "/tmp/catscript_demo.txt"
|
||||
local w = File::open(path, "w")
|
||||
w:write("line one\n")
|
||||
w:close()
|
||||
w.write("line one\n")
|
||||
w.close()
|
||||
|
||||
local r = File::open(path, "r")
|
||||
print("file:", r:read())
|
||||
r:close()
|
||||
print("file:", r.read())
|
||||
r.close()
|
||||
|
||||
-- type hints: advisory, including DynSized() and sentinel_sized
|
||||
function describe(n : cint, buf : cbyte[sentinel_sized<0>]) -> ascii_text
|
||||
|
||||
@@ -13,10 +13,10 @@ print("type:", type(count_up(3)))
|
||||
|
||||
-- manual iteration with next
|
||||
local g = count_up(3)
|
||||
print("next:", g:next())
|
||||
print("next:", g:next())
|
||||
print("next:", g:next())
|
||||
print("next:", g:next()) -- empty: exhausted
|
||||
print("next:", g.next())
|
||||
print("next:", g.next())
|
||||
print("next:", g.next())
|
||||
print("next:", g.next()) -- empty: exhausted
|
||||
|
||||
-- for..in drives any iterator automatically
|
||||
local sum = 0
|
||||
|
||||
+11
-11
@@ -8,26 +8,26 @@
|
||||
use glfw
|
||||
|
||||
local version = memory(12)
|
||||
glfw.get_version(version, version:ptr() + 4, version:ptr() + 8)
|
||||
print("glfw:", version:u32(0), version:u32(4), version:u32(8))
|
||||
glfw::get_version(version, version.ptr() + 4, version.ptr() + 8)
|
||||
print("glfw:", version.u32(0), version.u32(4), version.u32(8))
|
||||
|
||||
local ok = glfw.init()
|
||||
local ok = glfw::init()
|
||||
print("init:", ok)
|
||||
|
||||
-- a hidden window with no client API (for Vulkan surfaces)
|
||||
glfw.window_hint(glfw.visible, glfw.false)
|
||||
glfw.window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw.create_window(320, 240, "cat", 0, 0)
|
||||
glfw::window_hint(glfw.visible, glfw.false)
|
||||
glfw::window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw::create_window(320, 240, "cat", 0, 0)
|
||||
print("window:", win > 0)
|
||||
|
||||
if win > 0 then
|
||||
local fw = memory(8)
|
||||
local fh = memory(8)
|
||||
glfw.get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw:u32(0), fh:u32(0))
|
||||
print("should close:", glfw.window_should_close(win) == glfw.true)
|
||||
glfw.destroy_window(win)
|
||||
glfw::get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw.u32(0), fh.u32(0))
|
||||
print("should close:", glfw::window_should_close(win) == glfw.true)
|
||||
glfw::destroy_window(win)
|
||||
end
|
||||
|
||||
glfw.terminate()
|
||||
glfw::terminate()
|
||||
print("done")
|
||||
+3
-3
@@ -13,6 +13,6 @@ local counter = {
|
||||
end
|
||||
}
|
||||
|
||||
counter:increment(1)
|
||||
counter:increment(2)
|
||||
print("counter:", counter:get()) -- 3
|
||||
counter.increment(1)
|
||||
counter.increment(2)
|
||||
print("counter:", counter.get()) -- 3
|
||||
+2
-2
@@ -9,6 +9,6 @@ local f = File::open("/tmp/catscript_slots_demo.txt", "w")
|
||||
print("path:", f.path)
|
||||
print("mode:", f.mode)
|
||||
print("closed:", f.closed)
|
||||
f:write("slots")
|
||||
f:close()
|
||||
f.write("slots")
|
||||
f.close()
|
||||
print("closed:", f.closed)
|
||||
+10
-10
@@ -9,23 +9,23 @@ use vulkan
|
||||
|
||||
-- VkApplicationInfo
|
||||
local ai = memory(vulkan.size_vk_application_info)
|
||||
ai:fill(0)
|
||||
ai:set_u32(0, vulkan.structure_type_application_info)
|
||||
ai:set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
ai.fill(0)
|
||||
ai.set_u32(0, vulkan.structure_type_application_info)
|
||||
ai.set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
|
||||
-- VkInstanceCreateInfo
|
||||
local ci = memory(vulkan.size_vk_instance_create_info)
|
||||
ci:fill(0)
|
||||
ci:set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci:set_u64(24, ai:ptr()) -- pApplicationInfo
|
||||
ci.fill(0)
|
||||
ci.set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci.set_u64(24, ai.ptr()) -- pApplicationInfo
|
||||
|
||||
local inst = memory(8)
|
||||
local r = vulkan.create_instance(ci, 0, inst)
|
||||
local r = vulkan::create_instance(ci, 0, inst)
|
||||
print("create_instance:", r == vulkan.success)
|
||||
|
||||
local n = memory(4)
|
||||
vulkan.enumerate_physical_devices(inst:u64(0), n, 0)
|
||||
print("devices > 0:", n:u32(0) > 0)
|
||||
vulkan::enumerate_physical_devices(inst.u64(0), n, 0)
|
||||
print("devices > 0:", n.u32(0) > 0)
|
||||
|
||||
vulkan.destroy_instance(inst:u64(0), 0)
|
||||
vulkan::destroy_instance(inst.u64(0), 0)
|
||||
print("done")
|
||||
@@ -7,11 +7,11 @@
|
||||
* a declared signature, using libffi to perform the ABI-correct call.
|
||||
*
|
||||
* local lib = ffi.load("libm.so.6")
|
||||
* local pow = lib:func("pow", "double(double,double)")
|
||||
* local pow = lib.func("pow", "double(double,double)")
|
||||
* print(pow(2, 10)) -- 1024
|
||||
*
|
||||
* local c = ffi.load("libc.so.6")
|
||||
* local pid = c:func("getpid", "int()")
|
||||
* local pid = c.func("getpid", "int()")
|
||||
*
|
||||
* Signature grammar: "RET(ARG,ARG,...)" where each type is one of
|
||||
* void, bool, char, int8/uint8, int16/uint16, int/long/int32/uint32,
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "cat/value.h"
|
||||
|
||||
/* CAT_TYPES_FFI_Library -- a dlopen() handle as a first-class value.
|
||||
* `lib:func(name, sig)` / `lib[name]` resolve a symbol. */
|
||||
* `lib.func(name, sig)` / `lib[name]` resolve a symbol. */
|
||||
typedef struct CAT_TYPES_FFI_Library {
|
||||
CAT_VALUE_Value base;
|
||||
void *handle;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* BytesIO -- an in-memory byte buffer (read as bytes)
|
||||
*
|
||||
* `File` is also exposed to catscript as a class table so that
|
||||
* `File::open(path)` / `File:open(path)` produce a `File` value.
|
||||
* `File::open(path)` produces a `File` value (static, no self).
|
||||
*/
|
||||
#ifndef CAT_TYPES_FILE_H
|
||||
#define CAT_TYPES_FILE_H
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
* and for the whole VM run since values are not freed early).
|
||||
*
|
||||
* local m = memory(16)
|
||||
* m:set_u32(0, 1234)
|
||||
* print(m:u32(0)) -- 1234
|
||||
* m:set_text(4, "hi") -- 7 bytes: "hi" + NUL is at 4..6
|
||||
* print(m:text(4)) -- hi
|
||||
* m.set_u32(0, 1234)
|
||||
* print(m.u32(0)) -- 1234
|
||||
* m.set_text(4, "hi") -- 7 bytes: "hi" + NUL is at 4..6
|
||||
* print(m.text(4)) -- hi
|
||||
*/
|
||||
#ifndef CAT_TYPES_MEMORY_H
|
||||
#define CAT_TYPES_MEMORY_H
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
* compiler lowers surface syntax to ordinary method sends against a small
|
||||
* set of well-known selector names:
|
||||
*
|
||||
* obj(a, b) -> obj:_parenthesis_(a, b)
|
||||
* obj[i] -> obj:_squarebrackets_(i)
|
||||
* obj[i] = v -> obj:_squarebrackets_set_(i, v)
|
||||
* obj.k -> obj:_dot_("k")
|
||||
* obj.k = v -> obj:_dot_set_("k", v)
|
||||
* obj(a, b) -> obj._parenthesis_(a, b)
|
||||
* obj[i] -> obj._squarebrackets_(i)
|
||||
* obj[i] = v -> obj._squarebrackets_set_(i, v)
|
||||
* obj.k -> obj._dot_("k")
|
||||
* obj.k = v -> obj._dot_set_("k", v)
|
||||
* { ... } -> _curlybrackets_ hook on the new object
|
||||
*
|
||||
* Every object bound to these names inherits a default implementation
|
||||
|
||||
+66
-27
@@ -405,42 +405,81 @@ static void primary(Compiler *c)
|
||||
if (c->tok.kind == CAT_LEXER_TOK_NAME ||
|
||||
(c->tok.kind >= CAT_LEXER_TOK_AND &&
|
||||
c->tok.kind <= CAT_LEXER_TOK_EXTENDS)) {
|
||||
int k = add_string(c, c->tok.text);
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, c->tok.line);
|
||||
char mname[256];
|
||||
int mline = c->tok.line;
|
||||
int k;
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
k = add_string(c, mname);
|
||||
if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
/* a.b(args) -- method call WITH self */
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
emit(c, CAT_BYTECODE_OP_CALLMETHOD, nargs, k,
|
||||
mline);
|
||||
} else {
|
||||
/* a.b -- read the member (a field, or a function
|
||||
* value to be called later) */
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, mline);
|
||||
}
|
||||
}
|
||||
} else if ((check(c, CAT_LEXER_TOK_COLON) ||
|
||||
check(c, CAT_LEXER_TOK_DCOLON)) &&
|
||||
c->lx.src[c->lx.pos] != '\0') {
|
||||
/* method call: obj:name(args), or class method:
|
||||
* Class::name(args). Both call with the receiver as
|
||||
* the implicit first argument (`self`). */
|
||||
} else if (check(c, CAT_LEXER_TOK_DCOLON)) {
|
||||
/* a::b -- static access: no `self`. `a::b(args)` calls
|
||||
* `b` without the receiver; `a::b` reads the member. */
|
||||
next(c);
|
||||
if (c->tok.kind == CAT_LEXER_TOK_NAME ||
|
||||
(c->tok.kind >= CAT_LEXER_TOK_AND &&
|
||||
c->tok.kind <= CAT_LEXER_TOK_EXTENDS)) {
|
||||
char mname[256];
|
||||
int mline = c->tok.line;
|
||||
int k;
|
||||
snprintf(mname, sizeof(mname), "%s", c->tok.text);
|
||||
next(c);
|
||||
k = add_string(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_GETATTR, k, 0, mline);
|
||||
if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
/* call the member WITHOUT self */
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
emit(c, CAT_BYTECODE_OP_CALL, nargs, 0, mline);
|
||||
}
|
||||
}
|
||||
} else if (check(c, CAT_LEXER_TOK_COLON)) {
|
||||
/* The old `obj:method(args)` syntax was removed. Detect it
|
||||
* (name '.') and point the user at the new forms. */
|
||||
size_t sp = c->lx.pos;
|
||||
int sl = c->lx.line;
|
||||
CAT_LEXER_Token la;
|
||||
CAT_LEXER_Token la, nx;
|
||||
cat_lexer_next(&c->lx, &la);
|
||||
if (la.kind == CAT_LEXER_TOK_NAME) {
|
||||
char mname[256];
|
||||
int k, nargs = 0;
|
||||
snprintf(mname, sizeof(mname), "%s", la.text);
|
||||
c->tok = la;
|
||||
next(c);
|
||||
expect(c, CAT_LEXER_TOK_LPAREN, "'('");
|
||||
while (!check(c, CAT_LEXER_TOK_RPAREN) &&
|
||||
!check(c, CAT_LEXER_TOK_EOF)) {
|
||||
expr(c);
|
||||
nargs++;
|
||||
if (!accept(c, CAT_LEXER_TOK_COMMA))
|
||||
break;
|
||||
}
|
||||
expect(c, CAT_LEXER_TOK_RPAREN, "')'");
|
||||
k = add_string(c, mname);
|
||||
emit(c, CAT_BYTECODE_OP_CALLMETHOD, nargs, k, c->tok.line);
|
||||
} else {
|
||||
cat_lexer_next(&c->lx, &nx);
|
||||
c->lx.pos = sp;
|
||||
c->lx.line = sl;
|
||||
break;
|
||||
if (nx.kind == CAT_LEXER_TOK_LPAREN) {
|
||||
err_at(c, c->tok.line,
|
||||
"the ':' method syntax was removed; "
|
||||
"use '.' for obj.method(args) or "
|
||||
"'::' for obj::method(args)");
|
||||
c->failed = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} else if (check(c, CAT_LEXER_TOK_LPAREN)) {
|
||||
int nargs = 0;
|
||||
next(c);
|
||||
|
||||
+8
-8
@@ -281,16 +281,16 @@ int cat_lexer_next(CAT_LEXER_Lexer *lx, CAT_LEXER_Token *out)
|
||||
|
||||
const char *cat_lexer_kind_name(int kind)
|
||||
{
|
||||
/* Must match enum CAT_LEXER_Kind exactly (same order). */
|
||||
static const char *names[] = {
|
||||
"EOF", "NAME", "INT", "FLOAT", "STRING",
|
||||
"and", "break", "do", "else", "elseif", "end", "false", "for",
|
||||
"function", "if", "in", "local", "not", "or", "repeat",
|
||||
"return", "then", "true", "until", "while",
|
||||
"yield", "try", "except", "raise",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=", "<",
|
||||
">", "=", "(", ")", "{", "}", "[", "]", ";", ",", ".", ":",
|
||||
"::", "..", "->",
|
||||
"class", "extends", "use"
|
||||
"and", "break", "do", "else", "elseif", "end", "false",
|
||||
"for", "function", "if", "in", "local", "nil", "not",
|
||||
"or", "repeat", "return", "then", "true", "until", "while",
|
||||
"yield", "try", "except", "raise", "use", "class", "extends",
|
||||
"+", "-", "*", "/", "%", "^", "#", "==", "~=", "<=", ">=",
|
||||
"<", ">", "=", "(", ")", "{", "}", "[", "]", ";", ",",
|
||||
".", ":", "::", "..", "->"
|
||||
};
|
||||
if (kind >= 0 && kind < (int)(sizeof(names) / sizeof(names[0])))
|
||||
return names[kind];
|
||||
|
||||
+3
-3
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Naming rule: symbols here are cat_types_ffi_* / CAT_TYPES_FFI_*.
|
||||
*
|
||||
* `ffi.load(path)` dlopen()s a shared object. `lib:func(name, sig)`
|
||||
* `ffi.load(path)` dlopen()s a shared object. `lib.func(name, sig)`
|
||||
* dlsym()s a symbol and prepares a libffi call from the signature, e.g.
|
||||
* "double(double,double)". Calling the result makes the ABI-correct
|
||||
* native call and converts the result back to a catscript value.
|
||||
@@ -254,7 +254,7 @@ CAT_VALUE_Vtable *cat_types_ffi_function_vtable(void)
|
||||
|
||||
/* ---- the Library value ---------------------------------------------- */
|
||||
|
||||
/* lib:func(name, sig) -- the explicit form that carries the signature. */
|
||||
/* lib.func(name, sig) -- the explicit form that carries the signature. */
|
||||
static int lib_method_func(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -280,7 +280,7 @@ static int library_get_attr_full(CAT_VALUE_Value *self, const char *name,
|
||||
static int library_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
|
||||
int nargs, CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
/* Calling a library directly: lib("name", "sig") == lib:func(...) */
|
||||
/* Calling a library directly: lib("name", "sig") == lib.func(...) */
|
||||
CAT_VALUE_Value *callargs[4];
|
||||
|
||||
callargs[0] = self;
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
* f = File::open("x.txt") -- a real file
|
||||
* s = StringIO("hello") -- an in-memory text stream
|
||||
* b = BytesIO() -- an in-memory byte stream
|
||||
* f:write("data"); f:close()
|
||||
* s:read()
|
||||
* f.write("data"); f.close()
|
||||
* s.read()
|
||||
*
|
||||
* Thread-safety: every stream owns its own buffer/handle; nothing here
|
||||
* touches shared mutable state, so streams created and used on separate
|
||||
@@ -363,12 +363,14 @@ static int cm_open(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
const char *path, *mode;
|
||||
|
||||
(void)vm;
|
||||
if (nargs < 2) {
|
||||
/* `File::open(path, mode)` is static (`::`), so the receiver is NOT
|
||||
* passed: path is args[0] and mode args[1]. */
|
||||
if (nargs < 1) {
|
||||
*out = cat_types_empty();
|
||||
return 0;
|
||||
}
|
||||
path = as_text(args[1]);
|
||||
mode = (nargs > 2) ? as_text(args[2]) : "r";
|
||||
path = as_text(args[0]);
|
||||
mode = (nargs > 1) ? as_text(args[1]) : "r";
|
||||
if (mode == NULL)
|
||||
mode = "r";
|
||||
*out = cat_types_file_open(path, mode);
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
CAT_ERROR_Error *err);
|
||||
|
||||
/* `it:next()` method form: receiver is args[0]. */
|
||||
/* `it.next()` method form: receiver is args[0]. */
|
||||
static int gen_method_next(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out);
|
||||
|
||||
@@ -113,7 +113,7 @@ static int generator_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
return 1; /* exhausted */
|
||||
}
|
||||
|
||||
/* `it:next()` -- returns the next value, or empty when exhausted. */
|
||||
/* `it.next()` -- returns the next value, or empty when exhausted. */
|
||||
static int gen_method_next(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ static int list_next(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* list[i] / list:get(i) -- 0-based indexing. */
|
||||
/* list[i] / list.get(i) -- 0-based indexing. */
|
||||
static int list_get_attribute(CAT_VALUE_Value *self, const char *name,
|
||||
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
{
|
||||
|
||||
@@ -111,7 +111,7 @@ static int mem_set_u64(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_V
|
||||
static int mem_set_f32(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_Value **o){ return mem_set(a[0], a+1, n-1, o, &vm->err, 4, 1); }
|
||||
static int mem_set_f64(CAT_VM_State *vm, CAT_VALUE_Value **a, int n, CAT_VALUE_Value **o){ return mem_set(a[0], a+1, n-1, o, &vm->err, 8, 1); }
|
||||
|
||||
/* m:text(off) -- read a NUL-terminated string starting at off. */
|
||||
/* m.text(off) -- read a NUL-terminated string starting at off. */
|
||||
static int mem_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -126,7 +126,7 @@ static int mem_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:set_text(off, s) -- write s plus a terminating NUL. */
|
||||
/* m.set_text(off, s) -- write s plus a terminating NUL. */
|
||||
static int mem_set_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -144,7 +144,7 @@ static int mem_set_text(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:ptr() -- the address as an int (for an FFI call). */
|
||||
/* m.ptr() -- the address as an int (for an FFI call). */
|
||||
static int mem_ptr(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -154,7 +154,7 @@ static int mem_ptr(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:size() -- the buffer length. */
|
||||
/* m.size() -- the buffer length. */
|
||||
static int mem_size_(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -164,7 +164,7 @@ static int mem_size_(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:fill(byte [, off, count]) -- set bytes. */
|
||||
/* m.fill(byte [, off, count]) -- set bytes. */
|
||||
static int mem_fill(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
@@ -183,7 +183,7 @@ static int mem_fill(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* m:copy(dst_off, src_memory, src_off, count) -- bounded memcpy. */
|
||||
/* m.copy(dst_off, src_memory, src_off, count) -- bounded memcpy. */
|
||||
static int mem_copy(CAT_VM_State *vm, CAT_VALUE_Value **a, int n,
|
||||
CAT_VALUE_Value **out)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Naming rule: symbols here are cat_types_selector_* / CAT_TYPES_SELECTOR_*.
|
||||
*
|
||||
* Each selector is a bound default method. When a program sends
|
||||
* `obj:_dot_(k)` and the object does not define `_dot_`, the attribute
|
||||
* `obj._dot_(k)` and the object does not define `_dot_`, the attribute
|
||||
* dispatcher hands back one of these instead, and calling it performs the
|
||||
* raw built-in access. This is what lets the compiler drop the special
|
||||
* GETATTR/SETATTR/GETIDX/SETIDX/CALL cases: they are all just `:` sends.
|
||||
@@ -35,7 +35,7 @@ static void sel_key(CAT_VALUE_Value *v, char *buf, size_t n)
|
||||
buf[0] = '\0';
|
||||
}
|
||||
|
||||
/* A selector is called as `receiver:_sel_(...)`, so args[0] is the
|
||||
/* A selector is called as `receiver._sel_(...)`, so args[0] is the
|
||||
* receiver and args[1..] are the selector's own arguments. */
|
||||
static int selector_call(CAT_VALUE_Value *self, CAT_VALUE_Value **args,
|
||||
int nargs, CAT_VALUE_Value **out, CAT_ERROR_Error *err)
|
||||
|
||||
+6
-6
@@ -9,8 +9,8 @@ function fs_read(path)
|
||||
if f == empty then
|
||||
return ""
|
||||
end
|
||||
local data = f:read()
|
||||
f:close()
|
||||
local data = f.read()
|
||||
f.close()
|
||||
return data
|
||||
end
|
||||
|
||||
@@ -20,8 +20,8 @@ function fs_write(path, text)
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
f:close()
|
||||
f.write(text)
|
||||
f.close()
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -31,8 +31,8 @@ function fs_append(path, text)
|
||||
if f == empty then
|
||||
return false
|
||||
end
|
||||
f:write(text)
|
||||
f:close()
|
||||
f.write(text)
|
||||
f.close()
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
+124
-124
@@ -10,130 +10,130 @@ local _lib = ffi::load('libglfw.so.3')
|
||||
glfw = {}
|
||||
glfw.lib = _lib
|
||||
|
||||
glfw.init = _lib:func('glfwInit', 'int()')
|
||||
glfw.terminate = _lib:func('glfwTerminate', 'void()')
|
||||
glfw.init_hint = _lib:func('glfwInitHint', 'void(int,int)')
|
||||
glfw.init_allocator = _lib:func('glfwInitAllocator', 'void(ptr)')
|
||||
glfw.init_vulkan_loader = _lib:func('glfwInitVulkanLoader', 'void(int)')
|
||||
glfw.get_version = _lib:func('glfwGetVersion', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_version_string = _lib:func('glfwGetVersionString', 'string()')
|
||||
glfw.get_error = _lib:func('glfwGetError', 'int(ptr)')
|
||||
glfw.set_error_callback = _lib:func('glfwSetErrorCallback', 'int(int)')
|
||||
glfw.get_platform = _lib:func('glfwGetPlatform', 'int()')
|
||||
glfw.platform_supported = _lib:func('glfwPlatformSupported', 'int(int)')
|
||||
glfw.get_monitors = _lib:func('glfwGetMonitors', 'ptr(ptr)')
|
||||
glfw.get_primary_monitor = _lib:func('glfwGetPrimaryMonitor', 'ptr()')
|
||||
glfw.get_monitor_pos = _lib:func('glfwGetMonitorPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_workarea = _lib:func('glfwGetMonitorWorkarea', 'void(ptr,ptr,ptr,ptr,ptr)')
|
||||
glfw.get_monitor_physical_size = _lib:func('glfwGetMonitorPhysicalSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_content_scale = _lib:func('glfwGetMonitorContentScale', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_name = _lib:func('glfwGetMonitorName', 'string(ptr)')
|
||||
glfw.set_monitor_user_pointer = _lib:func('glfwSetMonitorUserPointer', 'void(ptr,ptr)')
|
||||
glfw.get_monitor_user_pointer = _lib:func('glfwGetMonitorUserPointer', 'ptr(ptr)')
|
||||
glfw.set_monitor_callback = _lib:func('glfwSetMonitorCallback', 'int(int)')
|
||||
glfw.get_video_modes = _lib:func('glfwGetVideoModes', 'ptr(ptr,ptr)')
|
||||
glfw.get_video_mode = _lib:func('glfwGetVideoMode', 'ptr(ptr)')
|
||||
glfw.set_gamma = _lib:func('glfwSetGamma', 'void(ptr,float)')
|
||||
glfw.get_gamma_ramp = _lib:func('glfwGetGammaRamp', 'ptr(ptr)')
|
||||
glfw.set_gamma_ramp = _lib:func('glfwSetGammaRamp', 'void(ptr,ptr)')
|
||||
glfw.default_window_hints = _lib:func('glfwDefaultWindowHints', 'void()')
|
||||
glfw.window_hint = _lib:func('glfwWindowHint', 'void(int,int)')
|
||||
glfw.window_hint_string = _lib:func('glfwWindowHintString', 'void(int,string)')
|
||||
glfw.create_window = _lib:func('glfwCreateWindow', 'ptr(int,int,string,ptr,ptr)')
|
||||
glfw.destroy_window = _lib:func('glfwDestroyWindow', 'void(ptr)')
|
||||
glfw.window_should_close = _lib:func('glfwWindowShouldClose', 'int(ptr)')
|
||||
glfw.set_window_should_close = _lib:func('glfwSetWindowShouldClose', 'void(ptr,int)')
|
||||
glfw.get_window_title = _lib:func('glfwGetWindowTitle', 'string(ptr)')
|
||||
glfw.set_window_title = _lib:func('glfwSetWindowTitle', 'void(ptr,string)')
|
||||
glfw.set_window_icon = _lib:func('glfwSetWindowIcon', 'void(ptr,int,ptr)')
|
||||
glfw.get_window_pos = _lib:func('glfwGetWindowPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_window_pos = _lib:func('glfwSetWindowPos', 'void(ptr,int,int)')
|
||||
glfw.get_window_size = _lib:func('glfwGetWindowSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_window_size_limits = _lib:func('glfwSetWindowSizeLimits', 'void(ptr,int,int,int,int)')
|
||||
glfw.set_window_aspect_ratio = _lib:func('glfwSetWindowAspectRatio', 'void(ptr,int,int)')
|
||||
glfw.set_window_size = _lib:func('glfwSetWindowSize', 'void(ptr,int,int)')
|
||||
glfw.get_framebuffer_size = _lib:func('glfwGetFramebufferSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_window_frame_size = _lib:func('glfwGetWindowFrameSize', 'void(ptr,ptr,ptr,ptr,ptr)')
|
||||
glfw.get_window_content_scale = _lib:func('glfwGetWindowContentScale', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_window_opacity = _lib:func('glfwGetWindowOpacity', 'float(ptr)')
|
||||
glfw.set_window_opacity = _lib:func('glfwSetWindowOpacity', 'void(ptr,float)')
|
||||
glfw.iconify_window = _lib:func('glfwIconifyWindow', 'void(ptr)')
|
||||
glfw.restore_window = _lib:func('glfwRestoreWindow', 'void(ptr)')
|
||||
glfw.maximize_window = _lib:func('glfwMaximizeWindow', 'void(ptr)')
|
||||
glfw.show_window = _lib:func('glfwShowWindow', 'void(ptr)')
|
||||
glfw.hide_window = _lib:func('glfwHideWindow', 'void(ptr)')
|
||||
glfw.focus_window = _lib:func('glfwFocusWindow', 'void(ptr)')
|
||||
glfw.request_window_attention = _lib:func('glfwRequestWindowAttention', 'void(ptr)')
|
||||
glfw.get_window_monitor = _lib:func('glfwGetWindowMonitor', 'ptr(ptr)')
|
||||
glfw.set_window_monitor = _lib:func('glfwSetWindowMonitor', 'void(ptr,ptr,int,int,int,int,int)')
|
||||
glfw.get_window_attrib = _lib:func('glfwGetWindowAttrib', 'int(ptr,int)')
|
||||
glfw.set_window_attrib = _lib:func('glfwSetWindowAttrib', 'void(ptr,int,int)')
|
||||
glfw.set_window_user_pointer = _lib:func('glfwSetWindowUserPointer', 'void(ptr,ptr)')
|
||||
glfw.get_window_user_pointer = _lib:func('glfwGetWindowUserPointer', 'ptr(ptr)')
|
||||
glfw.set_window_pos_callback = _lib:func('glfwSetWindowPosCallback', 'int(ptr,int)')
|
||||
glfw.set_window_size_callback = _lib:func('glfwSetWindowSizeCallback', 'int(ptr,int)')
|
||||
glfw.set_window_close_callback = _lib:func('glfwSetWindowCloseCallback', 'int(ptr,int)')
|
||||
glfw.set_window_refresh_callback = _lib:func('glfwSetWindowRefreshCallback', 'int(ptr,int)')
|
||||
glfw.set_window_focus_callback = _lib:func('glfwSetWindowFocusCallback', 'int(ptr,int)')
|
||||
glfw.set_window_iconify_callback = _lib:func('glfwSetWindowIconifyCallback', 'int(ptr,int)')
|
||||
glfw.set_window_maximize_callback = _lib:func('glfwSetWindowMaximizeCallback', 'int(ptr,int)')
|
||||
glfw.set_framebuffer_size_callback = _lib:func('glfwSetFramebufferSizeCallback', 'int(ptr,int)')
|
||||
glfw.set_window_content_scale_callback = _lib:func('glfwSetWindowContentScaleCallback', 'int(ptr,int)')
|
||||
glfw.poll_events = _lib:func('glfwPollEvents', 'void()')
|
||||
glfw.wait_events = _lib:func('glfwWaitEvents', 'void()')
|
||||
glfw.wait_events_timeout = _lib:func('glfwWaitEventsTimeout', 'void(double)')
|
||||
glfw.post_empty_event = _lib:func('glfwPostEmptyEvent', 'void()')
|
||||
glfw.get_input_mode = _lib:func('glfwGetInputMode', 'int(ptr,int)')
|
||||
glfw.set_input_mode = _lib:func('glfwSetInputMode', 'void(ptr,int,int)')
|
||||
glfw.raw_mouse_motion_supported = _lib:func('glfwRawMouseMotionSupported', 'int()')
|
||||
glfw.get_key_name = _lib:func('glfwGetKeyName', 'string(int,int)')
|
||||
glfw.get_key_scancode = _lib:func('glfwGetKeyScancode', 'int(int)')
|
||||
glfw.get_key = _lib:func('glfwGetKey', 'int(ptr,int)')
|
||||
glfw.get_mouse_button = _lib:func('glfwGetMouseButton', 'int(ptr,int)')
|
||||
glfw.get_cursor_pos = _lib:func('glfwGetCursorPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_cursor_pos = _lib:func('glfwSetCursorPos', 'void(ptr,double,double)')
|
||||
glfw.create_cursor = _lib:func('glfwCreateCursor', 'ptr(ptr,int,int)')
|
||||
glfw.create_standard_cursor = _lib:func('glfwCreateStandardCursor', 'ptr(int)')
|
||||
glfw.destroy_cursor = _lib:func('glfwDestroyCursor', 'void(ptr)')
|
||||
glfw.set_cursor = _lib:func('glfwSetCursor', 'void(ptr,ptr)')
|
||||
glfw.set_key_callback = _lib:func('glfwSetKeyCallback', 'int(ptr,int)')
|
||||
glfw.set_char_callback = _lib:func('glfwSetCharCallback', 'int(ptr,int)')
|
||||
glfw.set_char_mods_callback = _lib:func('glfwSetCharModsCallback', 'int(ptr,int)')
|
||||
glfw.set_mouse_button_callback = _lib:func('glfwSetMouseButtonCallback', 'int(ptr,int)')
|
||||
glfw.set_cursor_pos_callback = _lib:func('glfwSetCursorPosCallback', 'int(ptr,int)')
|
||||
glfw.set_cursor_enter_callback = _lib:func('glfwSetCursorEnterCallback', 'int(ptr,int)')
|
||||
glfw.set_scroll_callback = _lib:func('glfwSetScrollCallback', 'int(ptr,int)')
|
||||
glfw.set_drop_callback = _lib:func('glfwSetDropCallback', 'int(ptr,int)')
|
||||
glfw.joystick_present = _lib:func('glfwJoystickPresent', 'int(int)')
|
||||
glfw.get_joystick_axes = _lib:func('glfwGetJoystickAxes', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_buttons = _lib:func('glfwGetJoystickButtons', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_hats = _lib:func('glfwGetJoystickHats', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_name = _lib:func('glfwGetJoystickName', 'string(int)')
|
||||
glfw.get_joystick_guid = _lib:func('glfwGetJoystickGUID', 'string(int)')
|
||||
glfw.set_joystick_user_pointer = _lib:func('glfwSetJoystickUserPointer', 'void(int,ptr)')
|
||||
glfw.get_joystick_user_pointer = _lib:func('glfwGetJoystickUserPointer', 'ptr(int)')
|
||||
glfw.joystick_is_gamepad = _lib:func('glfwJoystickIsGamepad', 'int(int)')
|
||||
glfw.set_joystick_callback = _lib:func('glfwSetJoystickCallback', 'int(int)')
|
||||
glfw.update_gamepad_mappings = _lib:func('glfwUpdateGamepadMappings', 'int(string)')
|
||||
glfw.get_gamepad_name = _lib:func('glfwGetGamepadName', 'string(int)')
|
||||
glfw.get_gamepad_state = _lib:func('glfwGetGamepadState', 'int(int,ptr)')
|
||||
glfw.set_clipboard_string = _lib:func('glfwSetClipboardString', 'void(ptr,string)')
|
||||
glfw.get_clipboard_string = _lib:func('glfwGetClipboardString', 'string(ptr)')
|
||||
glfw.get_time = _lib:func('glfwGetTime', 'double()')
|
||||
glfw.set_time = _lib:func('glfwSetTime', 'void(double)')
|
||||
glfw.get_timer_value = _lib:func('glfwGetTimerValue', 'uint64()')
|
||||
glfw.get_timer_frequency = _lib:func('glfwGetTimerFrequency', 'uint64()')
|
||||
glfw.make_context_current = _lib:func('glfwMakeContextCurrent', 'void(ptr)')
|
||||
glfw.get_current_context = _lib:func('glfwGetCurrentContext', 'ptr()')
|
||||
glfw.swap_buffers = _lib:func('glfwSwapBuffers', 'void(ptr)')
|
||||
glfw.swap_interval = _lib:func('glfwSwapInterval', 'void(int)')
|
||||
glfw.extension_supported = _lib:func('glfwExtensionSupported', 'int(string)')
|
||||
glfw.get_proc_address = _lib:func('glfwGetProcAddress', 'int(string)')
|
||||
glfw.vulkan_supported = _lib:func('glfwVulkanSupported', 'int()')
|
||||
glfw.get_required_instance_extensions = _lib:func('glfwGetRequiredInstanceExtensions', 'ptr(ptr)')
|
||||
glfw.get_instance_proc_address = _lib:func('glfwGetInstanceProcAddress', 'int(int,string)')
|
||||
glfw.get_physical_device_presentation_support = _lib:func('glfwGetPhysicalDevicePresentationSupport', 'int(int,int,int)')
|
||||
glfw.create_window_surface = _lib:func('glfwCreateWindowSurface', 'int(int,ptr,ptr,ptr)')
|
||||
glfw.init = _lib.func('glfwInit', 'int()')
|
||||
glfw.terminate = _lib.func('glfwTerminate', 'void()')
|
||||
glfw.init_hint = _lib.func('glfwInitHint', 'void(int,int)')
|
||||
glfw.init_allocator = _lib.func('glfwInitAllocator', 'void(ptr)')
|
||||
glfw.init_vulkan_loader = _lib.func('glfwInitVulkanLoader', 'void(int)')
|
||||
glfw.get_version = _lib.func('glfwGetVersion', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_version_string = _lib.func('glfwGetVersionString', 'string()')
|
||||
glfw.get_error = _lib.func('glfwGetError', 'int(ptr)')
|
||||
glfw.set_error_callback = _lib.func('glfwSetErrorCallback', 'int(int)')
|
||||
glfw.get_platform = _lib.func('glfwGetPlatform', 'int()')
|
||||
glfw.platform_supported = _lib.func('glfwPlatformSupported', 'int(int)')
|
||||
glfw.get_monitors = _lib.func('glfwGetMonitors', 'ptr(ptr)')
|
||||
glfw.get_primary_monitor = _lib.func('glfwGetPrimaryMonitor', 'ptr()')
|
||||
glfw.get_monitor_pos = _lib.func('glfwGetMonitorPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_workarea = _lib.func('glfwGetMonitorWorkarea', 'void(ptr,ptr,ptr,ptr,ptr)')
|
||||
glfw.get_monitor_physical_size = _lib.func('glfwGetMonitorPhysicalSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_content_scale = _lib.func('glfwGetMonitorContentScale', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_monitor_name = _lib.func('glfwGetMonitorName', 'string(ptr)')
|
||||
glfw.set_monitor_user_pointer = _lib.func('glfwSetMonitorUserPointer', 'void(ptr,ptr)')
|
||||
glfw.get_monitor_user_pointer = _lib.func('glfwGetMonitorUserPointer', 'ptr(ptr)')
|
||||
glfw.set_monitor_callback = _lib.func('glfwSetMonitorCallback', 'int(int)')
|
||||
glfw.get_video_modes = _lib.func('glfwGetVideoModes', 'ptr(ptr,ptr)')
|
||||
glfw.get_video_mode = _lib.func('glfwGetVideoMode', 'ptr(ptr)')
|
||||
glfw.set_gamma = _lib.func('glfwSetGamma', 'void(ptr,float)')
|
||||
glfw.get_gamma_ramp = _lib.func('glfwGetGammaRamp', 'ptr(ptr)')
|
||||
glfw.set_gamma_ramp = _lib.func('glfwSetGammaRamp', 'void(ptr,ptr)')
|
||||
glfw.default_window_hints = _lib.func('glfwDefaultWindowHints', 'void()')
|
||||
glfw.window_hint = _lib.func('glfwWindowHint', 'void(int,int)')
|
||||
glfw.window_hint_string = _lib.func('glfwWindowHintString', 'void(int,string)')
|
||||
glfw.create_window = _lib.func('glfwCreateWindow', 'ptr(int,int,string,ptr,ptr)')
|
||||
glfw.destroy_window = _lib.func('glfwDestroyWindow', 'void(ptr)')
|
||||
glfw.window_should_close = _lib.func('glfwWindowShouldClose', 'int(ptr)')
|
||||
glfw.set_window_should_close = _lib.func('glfwSetWindowShouldClose', 'void(ptr,int)')
|
||||
glfw.get_window_title = _lib.func('glfwGetWindowTitle', 'string(ptr)')
|
||||
glfw.set_window_title = _lib.func('glfwSetWindowTitle', 'void(ptr,string)')
|
||||
glfw.set_window_icon = _lib.func('glfwSetWindowIcon', 'void(ptr,int,ptr)')
|
||||
glfw.get_window_pos = _lib.func('glfwGetWindowPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_window_pos = _lib.func('glfwSetWindowPos', 'void(ptr,int,int)')
|
||||
glfw.get_window_size = _lib.func('glfwGetWindowSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_window_size_limits = _lib.func('glfwSetWindowSizeLimits', 'void(ptr,int,int,int,int)')
|
||||
glfw.set_window_aspect_ratio = _lib.func('glfwSetWindowAspectRatio', 'void(ptr,int,int)')
|
||||
glfw.set_window_size = _lib.func('glfwSetWindowSize', 'void(ptr,int,int)')
|
||||
glfw.get_framebuffer_size = _lib.func('glfwGetFramebufferSize', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_window_frame_size = _lib.func('glfwGetWindowFrameSize', 'void(ptr,ptr,ptr,ptr,ptr)')
|
||||
glfw.get_window_content_scale = _lib.func('glfwGetWindowContentScale', 'void(ptr,ptr,ptr)')
|
||||
glfw.get_window_opacity = _lib.func('glfwGetWindowOpacity', 'float(ptr)')
|
||||
glfw.set_window_opacity = _lib.func('glfwSetWindowOpacity', 'void(ptr,float)')
|
||||
glfw.iconify_window = _lib.func('glfwIconifyWindow', 'void(ptr)')
|
||||
glfw.restore_window = _lib.func('glfwRestoreWindow', 'void(ptr)')
|
||||
glfw.maximize_window = _lib.func('glfwMaximizeWindow', 'void(ptr)')
|
||||
glfw.show_window = _lib.func('glfwShowWindow', 'void(ptr)')
|
||||
glfw.hide_window = _lib.func('glfwHideWindow', 'void(ptr)')
|
||||
glfw.focus_window = _lib.func('glfwFocusWindow', 'void(ptr)')
|
||||
glfw.request_window_attention = _lib.func('glfwRequestWindowAttention', 'void(ptr)')
|
||||
glfw.get_window_monitor = _lib.func('glfwGetWindowMonitor', 'ptr(ptr)')
|
||||
glfw.set_window_monitor = _lib.func('glfwSetWindowMonitor', 'void(ptr,ptr,int,int,int,int,int)')
|
||||
glfw.get_window_attrib = _lib.func('glfwGetWindowAttrib', 'int(ptr,int)')
|
||||
glfw.set_window_attrib = _lib.func('glfwSetWindowAttrib', 'void(ptr,int,int)')
|
||||
glfw.set_window_user_pointer = _lib.func('glfwSetWindowUserPointer', 'void(ptr,ptr)')
|
||||
glfw.get_window_user_pointer = _lib.func('glfwGetWindowUserPointer', 'ptr(ptr)')
|
||||
glfw.set_window_pos_callback = _lib.func('glfwSetWindowPosCallback', 'int(ptr,int)')
|
||||
glfw.set_window_size_callback = _lib.func('glfwSetWindowSizeCallback', 'int(ptr,int)')
|
||||
glfw.set_window_close_callback = _lib.func('glfwSetWindowCloseCallback', 'int(ptr,int)')
|
||||
glfw.set_window_refresh_callback = _lib.func('glfwSetWindowRefreshCallback', 'int(ptr,int)')
|
||||
glfw.set_window_focus_callback = _lib.func('glfwSetWindowFocusCallback', 'int(ptr,int)')
|
||||
glfw.set_window_iconify_callback = _lib.func('glfwSetWindowIconifyCallback', 'int(ptr,int)')
|
||||
glfw.set_window_maximize_callback = _lib.func('glfwSetWindowMaximizeCallback', 'int(ptr,int)')
|
||||
glfw.set_framebuffer_size_callback = _lib.func('glfwSetFramebufferSizeCallback', 'int(ptr,int)')
|
||||
glfw.set_window_content_scale_callback = _lib.func('glfwSetWindowContentScaleCallback', 'int(ptr,int)')
|
||||
glfw.poll_events = _lib.func('glfwPollEvents', 'void()')
|
||||
glfw.wait_events = _lib.func('glfwWaitEvents', 'void()')
|
||||
glfw.wait_events_timeout = _lib.func('glfwWaitEventsTimeout', 'void(double)')
|
||||
glfw.post_empty_event = _lib.func('glfwPostEmptyEvent', 'void()')
|
||||
glfw.get_input_mode = _lib.func('glfwGetInputMode', 'int(ptr,int)')
|
||||
glfw.set_input_mode = _lib.func('glfwSetInputMode', 'void(ptr,int,int)')
|
||||
glfw.raw_mouse_motion_supported = _lib.func('glfwRawMouseMotionSupported', 'int()')
|
||||
glfw.get_key_name = _lib.func('glfwGetKeyName', 'string(int,int)')
|
||||
glfw.get_key_scancode = _lib.func('glfwGetKeyScancode', 'int(int)')
|
||||
glfw.get_key = _lib.func('glfwGetKey', 'int(ptr,int)')
|
||||
glfw.get_mouse_button = _lib.func('glfwGetMouseButton', 'int(ptr,int)')
|
||||
glfw.get_cursor_pos = _lib.func('glfwGetCursorPos', 'void(ptr,ptr,ptr)')
|
||||
glfw.set_cursor_pos = _lib.func('glfwSetCursorPos', 'void(ptr,double,double)')
|
||||
glfw.create_cursor = _lib.func('glfwCreateCursor', 'ptr(ptr,int,int)')
|
||||
glfw.create_standard_cursor = _lib.func('glfwCreateStandardCursor', 'ptr(int)')
|
||||
glfw.destroy_cursor = _lib.func('glfwDestroyCursor', 'void(ptr)')
|
||||
glfw.set_cursor = _lib.func('glfwSetCursor', 'void(ptr,ptr)')
|
||||
glfw.set_key_callback = _lib.func('glfwSetKeyCallback', 'int(ptr,int)')
|
||||
glfw.set_char_callback = _lib.func('glfwSetCharCallback', 'int(ptr,int)')
|
||||
glfw.set_char_mods_callback = _lib.func('glfwSetCharModsCallback', 'int(ptr,int)')
|
||||
glfw.set_mouse_button_callback = _lib.func('glfwSetMouseButtonCallback', 'int(ptr,int)')
|
||||
glfw.set_cursor_pos_callback = _lib.func('glfwSetCursorPosCallback', 'int(ptr,int)')
|
||||
glfw.set_cursor_enter_callback = _lib.func('glfwSetCursorEnterCallback', 'int(ptr,int)')
|
||||
glfw.set_scroll_callback = _lib.func('glfwSetScrollCallback', 'int(ptr,int)')
|
||||
glfw.set_drop_callback = _lib.func('glfwSetDropCallback', 'int(ptr,int)')
|
||||
glfw.joystick_present = _lib.func('glfwJoystickPresent', 'int(int)')
|
||||
glfw.get_joystick_axes = _lib.func('glfwGetJoystickAxes', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_buttons = _lib.func('glfwGetJoystickButtons', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_hats = _lib.func('glfwGetJoystickHats', 'ptr(int,ptr)')
|
||||
glfw.get_joystick_name = _lib.func('glfwGetJoystickName', 'string(int)')
|
||||
glfw.get_joystick_guid = _lib.func('glfwGetJoystickGUID', 'string(int)')
|
||||
glfw.set_joystick_user_pointer = _lib.func('glfwSetJoystickUserPointer', 'void(int,ptr)')
|
||||
glfw.get_joystick_user_pointer = _lib.func('glfwGetJoystickUserPointer', 'ptr(int)')
|
||||
glfw.joystick_is_gamepad = _lib.func('glfwJoystickIsGamepad', 'int(int)')
|
||||
glfw.set_joystick_callback = _lib.func('glfwSetJoystickCallback', 'int(int)')
|
||||
glfw.update_gamepad_mappings = _lib.func('glfwUpdateGamepadMappings', 'int(string)')
|
||||
glfw.get_gamepad_name = _lib.func('glfwGetGamepadName', 'string(int)')
|
||||
glfw.get_gamepad_state = _lib.func('glfwGetGamepadState', 'int(int,ptr)')
|
||||
glfw.set_clipboard_string = _lib.func('glfwSetClipboardString', 'void(ptr,string)')
|
||||
glfw.get_clipboard_string = _lib.func('glfwGetClipboardString', 'string(ptr)')
|
||||
glfw.get_time = _lib.func('glfwGetTime', 'double()')
|
||||
glfw.set_time = _lib.func('glfwSetTime', 'void(double)')
|
||||
glfw.get_timer_value = _lib.func('glfwGetTimerValue', 'uint64()')
|
||||
glfw.get_timer_frequency = _lib.func('glfwGetTimerFrequency', 'uint64()')
|
||||
glfw.make_context_current = _lib.func('glfwMakeContextCurrent', 'void(ptr)')
|
||||
glfw.get_current_context = _lib.func('glfwGetCurrentContext', 'ptr()')
|
||||
glfw.swap_buffers = _lib.func('glfwSwapBuffers', 'void(ptr)')
|
||||
glfw.swap_interval = _lib.func('glfwSwapInterval', 'void(int)')
|
||||
glfw.extension_supported = _lib.func('glfwExtensionSupported', 'int(string)')
|
||||
glfw.get_proc_address = _lib.func('glfwGetProcAddress', 'int(string)')
|
||||
glfw.vulkan_supported = _lib.func('glfwVulkanSupported', 'int()')
|
||||
glfw.get_required_instance_extensions = _lib.func('glfwGetRequiredInstanceExtensions', 'ptr(ptr)')
|
||||
glfw.get_instance_proc_address = _lib.func('glfwGetInstanceProcAddress', 'int(int,string)')
|
||||
glfw.get_physical_device_presentation_support = _lib.func('glfwGetPhysicalDevicePresentationSupport', 'int(int,int,int)')
|
||||
glfw.create_window_surface = _lib.func('glfwCreateWindowSurface', 'int(int,ptr,ptr,ptr)')
|
||||
|
||||
-- constants
|
||||
glfw.version_major = 3
|
||||
|
||||
+17
-6
@@ -151,17 +151,28 @@ function par_suffixes(e)
|
||||
if par_at(".") then
|
||||
par_advance()
|
||||
local nm = par_expect("NAME", "attribute name")
|
||||
e = { t = "attr", obj = e, name = nm.text }
|
||||
if par_at("(") then
|
||||
-- obj.name(args): method call WITH self
|
||||
local args = par_args()
|
||||
e = { t = "method", obj = e, name = nm.text, args = args }
|
||||
else
|
||||
e = { t = "attr", obj = e, name = nm.text }
|
||||
end
|
||||
elseif par_at("::") then
|
||||
par_advance()
|
||||
local nm = par_expect("NAME", "static name")
|
||||
if par_at("(") then
|
||||
-- obj::name(args): call WITHOUT self
|
||||
local target = { t = "attr", obj = e, name = nm.text }
|
||||
e = { t = "call", f = target, args = par_args() }
|
||||
else
|
||||
e = { t = "attr", obj = e, name = nm.text }
|
||||
end
|
||||
elseif par_at("[") then
|
||||
par_advance()
|
||||
local idx = par_expr()
|
||||
par_expect("]", "']'")
|
||||
e = { t = "index", obj = e, idx = idx }
|
||||
elseif par_at(":") then
|
||||
par_advance()
|
||||
local nm = par_expect("NAME", "method name")
|
||||
local args = par_args()
|
||||
e = { t = "method", obj = e, name = nm.text, args = args }
|
||||
elseif par_at("(") then
|
||||
local args = par_args()
|
||||
e = { t = "call", f = e, args = args }
|
||||
|
||||
+650
-650
File diff suppressed because it is too large.
Load diff
+10
-10
@@ -12,37 +12,37 @@ print("bad load empty:", bad == empty)
|
||||
print("bad load type:", type(bad))
|
||||
|
||||
-- symbol binding
|
||||
local pow = m:func("pow", "double(double,double)")
|
||||
local pow = m.func("pow", "double(double,double)")
|
||||
print("pow bound:", pow ~= empty)
|
||||
print("missing sym empty:", m:func("no_such_symbol_xyz", "int()") == empty)
|
||||
print("missing sym empty:", m.func("no_such_symbol_xyz", "int()") == empty)
|
||||
|
||||
-- invalid signature -> empty
|
||||
print("bad sig empty:", m:func("pow", "not a signature") == empty)
|
||||
print("bad rettype empty:", m:func("pow", "nope(double)") == empty)
|
||||
print("bad sig empty:", m.func("pow", "not a signature") == empty)
|
||||
print("bad rettype empty:", m.func("pow", "nope(double)") == empty)
|
||||
|
||||
-- return conversions
|
||||
print("pow 2,10:", pow(2, 10))
|
||||
print("pow 3,4:", pow(3, 4))
|
||||
local floor = m:func("floor", "double(double)")
|
||||
local floor = m.func("floor", "double(double)")
|
||||
print("floor 3.7:", floor(3.7))
|
||||
local sqrt = m:func("sqrt", "double(double)")
|
||||
local sqrt = m.func("sqrt", "double(double)")
|
||||
print("sqrt 144:", sqrt(144))
|
||||
|
||||
-- int return
|
||||
local abs = c:func("abs", "int(int)")
|
||||
local abs = c.func("abs", "int(int)")
|
||||
print("abs -42:", abs(-42))
|
||||
|
||||
-- long + string arg
|
||||
local strlen = c:func("strlen", "long(string)")
|
||||
local strlen = c.func("strlen", "long(string)")
|
||||
print("strlen abc:", strlen("abc"))
|
||||
print("strlen empty:", strlen(""))
|
||||
|
||||
-- void return -> empty
|
||||
local srand = c:func("srand", "void(uint32)")
|
||||
local srand = c.func("srand", "void(uint32)")
|
||||
print("void ret empty:", srand(1) == empty)
|
||||
|
||||
-- pointer-ish spelling accepted
|
||||
local strchr = c:func("strchr", "string(string,int)")
|
||||
local strchr = c.func("strchr", "string(string,int)")
|
||||
print("strchr bound:", strchr ~= empty)
|
||||
|
||||
-- library is callable like :func
|
||||
|
||||
@@ -22,7 +22,7 @@ end
|
||||
Calc.add._doc_ = "add(a, b) on Calc."
|
||||
print("method doc:", Calc.add._doc_)
|
||||
local c = Calc()
|
||||
print("method call:", c:add(4, 5))
|
||||
print("method call:", c.add(4, 5))
|
||||
|
||||
-- help() reads a function's _doc_
|
||||
help(add)
|
||||
+11
-11
@@ -7,26 +7,26 @@
|
||||
use glfw
|
||||
|
||||
local version = memory(12)
|
||||
glfw.get_version(version, version:ptr() + 4, version:ptr() + 8)
|
||||
print("glfw:", version:u32(0), version:u32(4), version:u32(8))
|
||||
glfw::get_version(version, version.ptr() + 4, version.ptr() + 8)
|
||||
print("glfw:", version.u32(0), version.u32(4), version.u32(8))
|
||||
|
||||
local ok = glfw.init()
|
||||
local ok = glfw::init()
|
||||
print("init:", ok)
|
||||
|
||||
-- a hidden window with no client API (for Vulkan surfaces)
|
||||
glfw.window_hint(glfw.visible, glfw.false)
|
||||
glfw.window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw.create_window(320, 240, "cat", 0, 0)
|
||||
glfw::window_hint(glfw.visible, glfw.false)
|
||||
glfw::window_hint(0x00022001, 0) -- CLIENT_API = NO_API
|
||||
local win = glfw::create_window(320, 240, "cat", 0, 0)
|
||||
print("window:", win > 0)
|
||||
|
||||
if win > 0 then
|
||||
local fw = memory(8)
|
||||
local fh = memory(8)
|
||||
glfw.get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw:u32(0), fh:u32(0))
|
||||
print("should close:", glfw.window_should_close(win) == glfw.true)
|
||||
glfw.destroy_window(win)
|
||||
glfw::get_framebuffer_size(win, fw, fh)
|
||||
print("framebuffer:", fw.u32(0), fh.u32(0))
|
||||
print("should close:", glfw::window_should_close(win) == glfw.true)
|
||||
glfw::destroy_window(win)
|
||||
end
|
||||
|
||||
glfw.terminate()
|
||||
glfw::terminate()
|
||||
print("done")
|
||||
+16
-16
@@ -5,35 +5,35 @@
|
||||
-- corrupting memory.
|
||||
|
||||
local m = memory(16)
|
||||
m:set_u32(0, 1234)
|
||||
print("u32:", m:u32(0))
|
||||
m:set_u64(8, 9999999999)
|
||||
print("u64:", m:u64(8))
|
||||
m:set_text(4, "hi")
|
||||
print("text:", m:text(4))
|
||||
print("size:", m:size())
|
||||
m:set_f32(0, 3.5)
|
||||
print("f32:", m:f32(0))
|
||||
m:set_f64(8, 2.5)
|
||||
print("f64:", m:f64(8))
|
||||
m.set_u32(0, 1234)
|
||||
print("u32:", m.u32(0))
|
||||
m.set_u64(8, 9999999999)
|
||||
print("u64:", m.u64(8))
|
||||
m.set_text(4, "hi")
|
||||
print("text:", m.text(4))
|
||||
print("size:", m.size())
|
||||
m.set_f32(0, 3.5)
|
||||
print("f32:", m.f32(0))
|
||||
m.set_f64(8, 2.5)
|
||||
print("f64:", m.f64(8))
|
||||
|
||||
-- zeroed on allocation
|
||||
local z = memory(4)
|
||||
print("zero:", z:u32(0))
|
||||
print("zero:", z.u32(0))
|
||||
|
||||
-- a freed-pointer-style address is available for FFI calls
|
||||
local p = m:ptr()
|
||||
local p = m.ptr()
|
||||
print("ptr:", p > 0)
|
||||
|
||||
-- fill and re-read
|
||||
local f = memory(8)
|
||||
f:fill(0xAB)
|
||||
print("fill:", f:u8(0))
|
||||
f.fill(0xAB)
|
||||
print("fill:", f.u8(0))
|
||||
|
||||
-- out-of-bounds raises (caught here)
|
||||
try
|
||||
local small = memory(2)
|
||||
small:u32(0)
|
||||
small.u32(0)
|
||||
print("no raise")
|
||||
except e
|
||||
print("caught oob")
|
||||
|
||||
@@ -174,6 +174,15 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -
|
||||
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
|
||||
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
|
||||
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
|
||||
# ---- selectors: a.b (with self) / a::b (no self) ----------------------
|
||||
|
||||
out=`$BIN tests/selectors.cat`
|
||||
check "sel-self" "with self: 7" "`echo "$out" | sed -n 1p`"
|
||||
check "sel-field" "field x: 10" "`echo "$out" | sed -n 2p`"
|
||||
check "sel-static" "static: 42" "`echo "$out" | sed -n 3p`"
|
||||
check "sel-method" "self=obj" "`echo "$out" | sed -n 4p`"
|
||||
check "sel-noself" "static no self: 5" "`echo "$out" | sed -n 5p`"
|
||||
|
||||
# ---- GPU bindings (GLFW + Vulkan) ------------------------------------
|
||||
|
||||
if command -v xvfb-run >/dev/null 2>&1 && [ -f vendor/lib/libglfw.so.3 ]; then
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
-- tests/selectors.cat -- the a.b / a::b selector syntax.
|
||||
--
|
||||
-- a.b -- read the member `b` of `a`
|
||||
-- a.b(args) -- call member `b` WITH `a` as self
|
||||
-- a::b -- static read (no self)
|
||||
-- a::b(args) -- call `b` WITHOUT self
|
||||
--
|
||||
-- `a:b` is gone.
|
||||
|
||||
local counter = { n = 0 }
|
||||
counter.bump = function(self, by)
|
||||
self.n = self.n + by
|
||||
return self.n
|
||||
end
|
||||
|
||||
counter.bump(3)
|
||||
counter.bump(4)
|
||||
print("with self:", counter.n)
|
||||
|
||||
-- a plain table field
|
||||
local point = { x = 10, y = 20 }
|
||||
print("field x:", point.x)
|
||||
|
||||
-- static access: no receiver is passed
|
||||
local util = { twice = function(x) return x * 2 end }
|
||||
print("static:", util::twice(21))
|
||||
|
||||
-- method vs static on the same member
|
||||
local obj = { val = "obj" }
|
||||
obj.who = function(self) return "self=" .. self.val end
|
||||
print(obj.who())
|
||||
|
||||
-- static form passes no receiver: the first arg is the real first arg
|
||||
local math2 = { add = function(a, b) return a + b end }
|
||||
print("static no self:", math2::add(2, 3))
|
||||
+10
-10
@@ -8,23 +8,23 @@ use vulkan
|
||||
|
||||
-- VkApplicationInfo
|
||||
local ai = memory(vulkan.size_vk_application_info)
|
||||
ai:fill(0)
|
||||
ai:set_u32(0, vulkan.structure_type_application_info)
|
||||
ai:set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
ai.fill(0)
|
||||
ai.set_u32(0, vulkan.structure_type_application_info)
|
||||
ai.set_u32(24, 0x00400000) -- apiVersion 1.0.0
|
||||
|
||||
-- VkInstanceCreateInfo
|
||||
local ci = memory(vulkan.size_vk_instance_create_info)
|
||||
ci:fill(0)
|
||||
ci:set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci:set_u64(24, ai:ptr()) -- pApplicationInfo
|
||||
ci.fill(0)
|
||||
ci.set_u32(0, vulkan.structure_type_instance_create_info)
|
||||
ci.set_u64(24, ai.ptr()) -- pApplicationInfo
|
||||
|
||||
local inst = memory(8)
|
||||
local r = vulkan.create_instance(ci, 0, inst)
|
||||
local r = vulkan::create_instance(ci, 0, inst)
|
||||
print("create_instance:", r == vulkan.success)
|
||||
|
||||
local n = memory(4)
|
||||
vulkan.enumerate_physical_devices(inst:u64(0), n, 0)
|
||||
print("devices > 0:", n:u32(0) > 0)
|
||||
vulkan::enumerate_physical_devices(inst.u64(0), n, 0)
|
||||
print("devices > 0:", n.u32(0) > 0)
|
||||
|
||||
vulkan.destroy_instance(inst:u64(0), 0)
|
||||
vulkan::destroy_instance(inst.u64(0), 0)
|
||||
print("done")
|
||||
+1
-1
@@ -102,7 +102,7 @@ def main():
|
||||
ret_ffi = ctype_to_ffi(ret, is_return=True)
|
||||
sig = "%s(%s)" % (ret_ffi, ','.join(arglist))
|
||||
py_name = snake(name)
|
||||
print("glfw.%s = _lib:func(%r, %r)" % (py_name, name, sig))
|
||||
print("glfw.%s = _lib.func(%r, %r)" % (py_name, name, sig))
|
||||
count += 1
|
||||
print()
|
||||
|
||||
|
||||
+1
-1
@@ -263,7 +263,7 @@ def main():
|
||||
for name, ret, params in commands:
|
||||
sig = "%s(%s)" % (ffi_type(ret, lay),
|
||||
','.join(ffi_type(p, lay) for p in params))
|
||||
out.write("vulkan.%s = _lib:func(%r, %r)\n" % (snake(name), name, sig))
|
||||
out.write("vulkan.%s = _lib.func(%r, %r)\n" % (snake(name), name, sig))
|
||||
out.write("\n")
|
||||
|
||||
out.write("-- struct sizes (allocate with memory(vulkan.size_<name>))\n")
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""One-shot migration for the selector-syntax change.
|
||||
|
||||
Old:
|
||||
X:Y(args) -> method call WITH self
|
||||
X.Y(args) -> read member, then call (NO self)
|
||||
New:
|
||||
X.Y(args) -> method call WITH self
|
||||
X::Y(args) -> call WITHOUT self
|
||||
|
||||
So each old call form maps to a new one, preserving semantics. The
|
||||
substitution runs in a single pass so `X:Y(` -> `X.Y(` is not then
|
||||
re-matched and turned into `X::Y(`.
|
||||
|
||||
Usage: migrate_selectors.py FILE...
|
||||
"""
|
||||
import re
|
||||
import sys
|
||||
|
||||
CALL = re.compile(r'([A-Za-z_][A-Za-z0-9_]*)([:.])([A-Za-z_][A-Za-z0-9_]*)(\s*\()')
|
||||
|
||||
|
||||
def convert(text):
|
||||
def sub(m):
|
||||
recv, sep, name, paren = m.groups()
|
||||
if sep == ':':
|
||||
return "%s.%s%s" % (recv, name, paren) # with self
|
||||
return "%s::%s%s" % (recv, name, paren) # without self
|
||||
return CALL.sub(sub, text)
|
||||
|
||||
|
||||
def main():
|
||||
for path in sys.argv[1:]:
|
||||
with open(path) as f:
|
||||
src = f.read()
|
||||
out = convert(src)
|
||||
if out != src:
|
||||
with open(path, "w") as f:
|
||||
f.write(out)
|
||||
print("migrated", path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user