Skip to content

Commit

Permalink
fix: Fixed bad bz_newUserData jit signature
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 27, 2024
1 parent 8b21c14 commit 7e520fa
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 109 deletions.
44 changes: 22 additions & 22 deletions examples/game-of-life.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import "examples/sdl-wrapped" as _;

// buzz -L/path/to/SDL2.dylib/so/dll examples/game-of-life.buzz
object World {
int width,
int height,
[bool] cells,
width: int,
height: int,
cells: [bool],

static fun init(int width, int height) > World {
static fun init(width: int, height: int) > World {
var world = World{
width ,
height,
cells = [<bool>],
};

for (int i = 0; i < width * height; i = i + 1) {
for (i: int = 0; i < width * height; i = i + 1) {
world.cells.append(std\random(max: 5) == 1);
}

return world;
}

fun neighbors(int x, int y) > int {
int liveCount = 0;
for (int dy = y - 1; dy <= y + 1; dy = dy + 1) {
for (int dx = x - 1; dx <= x + 1; dx = dx + 1) {
fun neighbors(x: int, y: int) > int {
var liveCount = 0;
for (dy: int = y - 1; dy <= y + 1; dy = dy + 1) {
for (dx: int = x - 1; dx <= x + 1; dx = dx + 1) {
if (dy > 0 and dy > 0 and dx < this.width and dy < this.height and this.cells[dy * this.width + dx]) {
liveCount = liveCount + 1;
}
Expand All @@ -37,10 +37,10 @@ object World {

fun step() > void {
var cells = [<bool>];
for (int y = 0; y < this.height; y = y + 1) {
for (int x = 0; x < this.width; x = x + 1) {
const int coordinate = y * this.width + x;
const int liveNeighbors = this.neighbors(x: x, y: y);
for (y: int = 0; y < this.height; y = y + 1) {
for (x: int = 0; x < this.width; x = x + 1) {
const coordinate = y * this.width + x;
const liveNeighbors = this.neighbors(x: x, y: y);

cells.append(liveNeighbors == 3 or (this.cells[coordinate] and liveNeighbors == 2));
}
Expand All @@ -50,8 +50,8 @@ object World {
}

fun dump() > void {
for (int y = 0; y < this.height; y = y + 1) {
for (int x = 0; x < this.width; x = x + 1) {
for (y: int = 0; y < this.height; y = y + 1) {
for (x: int = 0; x < this.width; x = x + 1) {
io\stdout.write(
if (this.cells[y * this.width + x])
"x"
Expand All @@ -63,12 +63,12 @@ object World {
}
}

fun draw(Renderer renderer) > void !> SDLError {
fun draw(renderer: Renderer) > void !> SDLError {
const cellWidth = 800 / this.width;
const cellHeight = 600 / this.height;

for (int y = 0; y < this.height; y = y + 1) {
for (int x = 0; x < this.width; x = x + 1) {
for (y: int = 0; y < this.height; y = y + 1) {
for (x: int = 0; x < this.width; x = x + 1) {
renderer.setRenderDrawColor(
if (this.cells[y * this.width + x])
Color{
Expand Down Expand Up @@ -106,7 +106,7 @@ object World {
}
}

fun loop(World world, SDL sdl, Renderer renderer, Texture texture) > void !> SDLError {
fun loop(world: World, sdl: SDL, renderer: Renderer, texture: Texture) > void !> SDLError {
var dt = sdl.ticks();
while (true) {
if (sdl.pollEvent() -> event) {
Expand All @@ -132,7 +132,7 @@ fun loop(World world, SDL sdl, Renderer renderer, Texture texture) > void !> SDL
}
}

fun main([str] args) > void !> SDLError {
fun main(args: [str]) > void !> SDLError {
var sdl = SDL.init([Subsystem.Video]);

var window = Window.init(
Expand All @@ -154,8 +154,8 @@ fun main([str] args) > void !> SDLError {
height: 600,
);

int? width = if (args.len() > 0) std\parseInt(args[0]) else null;
int? height = if (args.len() > 1) std\parseInt(args[1]) else null;
const width = if (args.len() > 0) std\parseInt(args[0]) else null;
const height = if (args.len() > 1) std\parseInt(args[1]) else null;

var world = World.init(
width: width ?? 10,
Expand Down
106 changes: 53 additions & 53 deletions examples/sqlite.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace sqlite;

import "ffi";
import "std";
import "buffer" as _;
import "serialize" as _;
import "buffer";
import "serialize";

zdef("sqlite3", `
fn sqlite3_initialize() c_int;
fn sqlite3_shutdown() c_int;
// Right now we can't express a ptr to an opaque struct because we need to know its size
fn sqlite3_open_v2(filename: [*:0]const u8, ppDb: **anyopaque, flags: c_int, zVfs\ ?[*:0]const u8) c_int;
fn sqlite3_open_v2(filename: [*:0]const u8, ppDb: **anyopaque, flags: c_int, zVfs: ?[*:0]const u8) c_int;
fn sqlite3_close_v2(db: *anyopaque) c_int;
fn sqlite3_errmsg(db: *anyopaque) [*:0]const u8;
fn sqlite3_prepare_v2(db: *anyopaque, zSql: [*:0]const u8, nBytes: c_int, ppStmt: **anyopaque, pzTail: ?*[*]const u8) c_int;
Expand Down Expand Up @@ -60,8 +60,8 @@ enum(int) ResultCode {
}

export object SQLiteError {
ResultCode code,
str? message = "Error occured while interacting with SQLite database",
code: ResultCode,
message: str? = "Error occured while interacting with SQLite database",
}

export enum(int) OpenFlag {
Expand Down Expand Up @@ -98,65 +98,65 @@ enum(int) Type {
}

export object Query {
str query = "",
query: str = "",

fun branch(str keyword, str expression) > Query {
fun branch(keyword: str, expression: str) > Query {
return Query{
query = "{this.query} {keyword.upper()} {expression}"
};
}

fun select([str] columns) > Query {
fun select(columns: [str]) > Query {
return this.branch("select", expression: columns.join(", "));
}

fun delete(str expression) > Query {
fun delete(expression: str) > Query {
return this.branch("delete", expression: expression);
}

fun insertInto(str table, [str] columns) > Query {
fun insertInto(table: str, columns: [str]) > Query {
return this.branch("insert into", expression: "{table} ({columns.join(", ")})");
}

fun @"from"(str expression) > Query {
fun @"from"(expression: str) > Query {
return this.branch("from", expression: expression);
}

fun where(str expression) > Query {
fun where(expression: str) > Query {
return this.branch("where", expression: expression);
}

fun orderBy(str expression) > Query {
fun orderBy(expression: str) > Query {
return this.branch("order by", expression: expression);
}

fun groupBy(str expression) > Query {
fun groupBy(expression: str) > Query {
return this.branch("group by", expression: expression);
}

fun values([str] values) > Query {
fun values(values: [str]) > Query {
return this.branch("values", expression: "({values.join(", ")})");
}

fun prepare(Database db) > Statement !> SQLiteError {
fun prepare(db: Database) > Statement !> SQLiteError {
return db.prepare(this);
}

fun execute(Database db) > [[Boxed]] *> [Boxed]? !> SQLiteError {
fun execute(db: Database) > [[Boxed]] *> [Boxed]? !> SQLiteError {
return db.prepare(this).execute();
}
}

export object Statement {
str query,
ud db,
ud stmt,
query: str,
db: ud,
stmt: ud,

fun execute() > [[Boxed]] *> [Boxed]? !> SQLiteError {
std\print("Executing query `{this.query}`");
ResultCode code = ResultCode.Ok;
var code: ResultCode = ResultCode.Ok;

[[Boxed]] rows = [<[Boxed]>];
const rows = [<[Boxed]>];
while (code != ResultCode.Done) {
code = ResultCode(sqlite3_step(this.stmt)) ?? ResultCode.Error;
if (code != ResultCode.Ok and code != ResultCode.Row and code != ResultCode.Done) {
Expand All @@ -170,10 +170,10 @@ export object Statement {
break;
}

const int columnCount = sqlite3_column_count(this.stmt);
[any] row = [<any>];
for (int i = 0; i < columnCount; i = i + 1) {
const Type columnType = Type(sqlite3_column_type(this.stmt, col: i)) ?? Type.Null;
const columnCount = sqlite3_column_count(this.stmt);
var row = [<any>];
for (i: int = 0; i < columnCount; i = i + 1) {
const columnType = Type(sqlite3_column_type(this.stmt, col: i)) ?? Type.Null;

if (columnType == Type.Integer) {
row.append(
Expand All @@ -196,7 +196,7 @@ export object Statement {
}
}

const [Boxed] boxedRow = (Boxed.init(row) catch void).listValue();
const boxedRow = (Boxed.init(row) catch void).listValue();
rows.append(boxedRow);

_ = yield boxedRow;
Expand All @@ -211,26 +211,26 @@ export object Statement {
}

export object Database {
ud db,
db: ud,

fun collect() > void {
_ = ResultCode(sqlite3_close_v2(this.db)) ?? ResultCode.Error;
}

fun prepare(Query query) > Statement !> SQLiteError {
fun prepare(query: Query) > Statement !> SQLiteError {
return this.prepareRaw(query.query);
}

fun prepareRaw(str query) > Statement !> SQLiteError {
Buffer buffer = Buffer.init();
buffer\writeUserData(std\toUd(0)) catch void;
fun prepareRaw(query: str) > Statement !> SQLiteError {
const buffer = buffer\Buffer.init();
buffer.writeUserData(std\toUd(0)) catch void;

const ResultCode code = ResultCode(
const code = ResultCode(
sqlite3_prepare_v2(
db: this.db,
zSql: ffi\cstr(query),
nBytes: -1,
ppStmt: buffer\ptr(),
ppStmt: buffer.ptr(),
pzTail: null,
)
) ?? ResultCode.Error;
Expand All @@ -243,15 +243,15 @@ export object Database {

return Statement{
query = query,
stmt = buffer\readUserData()!,
stmt = buffer.readUserData()!,
db = this.db,
};
}
}

export object SQLite {
static fun init() > SQLite !> SQLiteError {
const ResultCode code = ResultCode(sqlite3_initialize()) ?? ResultCode.Error;
const code = ResultCode(sqlite3_initialize()) ?? ResultCode.Error;
if (code != ResultCode.Ok) {
throw SQLiteError{
code = code,
Expand All @@ -262,29 +262,29 @@ export object SQLite {
return SQLite{};
}

static fun escape(str string) > str {
static fun escape(string: str) > str {
return sqlite3_mprintf(ffi\cstr(string)) ?? "";
}

fun collect() > void {
_ = sqlite3_shutdown();
}

fun open(str filename, [OpenFlag] flags) > Database !> SQLiteError {
int cFlags = 0;
foreach (OpenFlag flag in flags) {
fun open(filename: str, flags: [OpenFlag]) > Database !> SQLiteError {
var cFlags = 0;
foreach (flag in flags) {
cFlags = cFlags | flag.value;
}

Buffer buffer = Buffer.init();
buffer\writeUserData(std\toUd(0)) catch void;
const buffer = buffer\Buffer.init();
buffer.writeUserData(std\toUd(0)) catch void;

const ResultCode code = ResultCode(
const code = ResultCode(
sqlite3_open_v2(
filename: ffi\cstr(filename),
ppDb: buffer\ptr(),
ppDb: buffer.ptr(),
flags: cFlags,
zVfs\ null
zVfs: null
)
) ?? ResultCode.Error;

Expand All @@ -298,37 +298,37 @@ export object SQLite {
std\print("Database `{filename}` opened");

return Database {
db = buffer\readUserData()!,
db = buffer.readUserData()!,
};
}
}

test "Testing sqlite" {
SQLite sqlite = SQLite.init();
const sqlite = SQLite.init();

Database database = sqlite.open("./test.db", flags: [OpenFlag.ReadWrite, OpenFlag.Create]);
const database = sqlite.open("./test.db", flags: [OpenFlag.ReadWrite, OpenFlag.Create]);

Statement statement = database.prepareRaw(`
const statement = database.prepareRaw(`
CREATE TABLE IF NOT EXISTS test (
name TEXT NOT NULL
)
`);

_ = statement.execute();

foreach (str name in ["john", "joe", "janet", "joline"]) {
foreach (name in ["john", "joe", "janet", "joline"]) {
_ = Query{}
.insertInto("test", columns: [ "name" ])
.values([ "'{name}'" ])
.execute(database);
}

Statement select = Query{}
const select = Query{}
.select([ "rowid", "name" ])
.@"from"("test")
.prepare(database);

foreach ([Boxed] row in &select.execute()) {
std\print("{row[0].integerValue()}: {row[1].stringValue()}");
foreach (row in &select.execute()) {
std\print("{row?[0].integerValue()}: {row?[1].stringValue()}");
}
}
Loading

0 comments on commit 7e520fa

Please sign in to comment.