Skip to content

Commit

Permalink
clean up deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Dec 31, 2024
1 parent 8275816 commit cf7d1ea
Show file tree
Hide file tree
Showing 9 changed files with 1,030 additions and 168 deletions.
888 changes: 826 additions & 62 deletions build.zig

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
"README.md",
},
.dependencies = .{
.tracer = .{
.url = "https://github.com/Rexicon226/zig-tracer/archive/6400e066bb3849be4906f3b6e124d6c8c5a5083f.tar.gz",
.hash = "122057453dba0b0cf6f07610d692cc7ab3f1d66defa150c936b398144812564a0c08",
// .libvaxis = .{
// .url = "https://github.com/rockorager/libvaxis/archive/c213919849114ce03def64806af546b9fc391859.tar.gz",
// .hash = "12206f2265ca2262f919ec125ff7fe15fa6f3be622806e211d7a9bd3055ba51a0908",
// .lazy = true,
// },
.libz = .{
.url = "git+https://github.com/allyourcodebase/zlib#0918e87b7629b9c6a50a08edd0ce30d849758faf",
.hash = "122034ab2a12adf8016ffa76e48b4be3245ffd305193edba4d83058adbcfa749c107",
},
.libgc = .{
.url = "https://github.com/Rexicon226/zig-libgc/archive/1ea12e140ec1471af5d15dae71b225764c9747ba.tar.gz",
.hash = "12209ad7282d9422973afe3e468e2f867bb102d04fa446a1d1c94f900bda94b7c68e",
.openssl = .{
.url = "git+https://github.com/kassane/openssl-zig#0cbd913c1fb26fcce1b0b31e82d9b880224515ed",
.hash = "1220162cad408b6f7eeb6b49733e75ebdb684da77d15bc24d04114b8aaaaa4d7ded6",
},
.libvaxis = .{
.url = "https://github.com/rockorager/libvaxis/archive/c213919849114ce03def64806af546b9fc391859.tar.gz",
.hash = "12206f2265ca2262f919ec125ff7fe15fa6f3be622806e211d7a9bd3055ba51a0908",
.lazy = true,
},
.cpython = .{
.url = "https://github.com/Rexicon226/zig-cpython/archive/969708680122dc4548d64adfae041261e2af480c.tar.gz",
.hash = "1220240c0fd3b1b062483c3f9f41b610181cdf70a47e047ae6379247178b20c34024",
.python = .{
.url = "git+https://github.com/Rexicon226/cpython#9b10a35f913f9f0c2f6356a8d8eef19b10b811b9",
.hash = "122027a5b201ec3f02bb6acb2c148fbbd0af9c22bf67df978ebf63203e1ff88a81c1",
},
},
}
2 changes: 0 additions & 2 deletions src/analysis/Graph.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
// SPDX-License-Identifier: GPL-3.0-only

//! Creates a temporal graph of a CodeObject

const std = @import("std");
const CodeObject = @import("../compiler/CodeObject.zig");
const Instruction = @import("../compiler/Instruction.zig");
Expand Down
159 changes: 159 additions & 0 deletions src/bindings.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
const builtin = @import("builtin");
pub const wchar = if (builtin.os.tag == .windows) u16 else u32;

pub const Py_file_input: c_int = 257;
pub const Py_MARSHAL_VERSION: c_int = 4;

pub const PyConfig = extern struct {
_config_init: c_int,

isolated: c_int,
use_environment: c_int,
dev_mode: c_int,
install_signal_handlers: c_int,
use_hash_seed: c_int,
hash_seed: u64,
faulthandler: c_int,
tracemalloc: c_int,
import_time: c_int,
show_ref_count: c_int,
dump_refs: c_int,
malloc_stats: c_int,
filesystem_encoding: [*:0]wchar,
filesystem_errors: [*:0]wchar,
pycache_prefix: [*:0]wchar,
parse_argv: c_int,
orig_argv: PyWideStringList,
argv: PyWideStringList,
xoptions: PyWideStringList,
warnoptions: PyWideStringList,
site_import: c_int,
bytes_warning: c_int,
warn_default_encoding: c_int,
inspect: c_int,
interactive: c_int,
optimization_level: c_int,
parser_debug: c_int,
write_bytecode: c_int,
verbose: c_int,
quiet: c_int,
user_site_directory: c_int,
configure_c_stdio: c_int,
buffered_stdio: c_int,
stdio_encoding: [*:0]wchar,
stdio_errors: [*:0]wchar,
check_hash_pycs_mode: [*:0]wchar,

// --- Path configuration inputs ------------
pathconfig_warnings: c_int,
program_name: [*:0]wchar,
pythonpath_env: [*:0]wchar,
home: [*:0]wchar,
platlibdir: [*:0]wchar,

// --- Path configuration outputs -----------
module_search_paths_set: c_int,
module_search_paths: PyWideStringList,
executable: [*:0]wchar,
base_executable: [*:0]wchar,
prefix: [*:0]wchar,
base_prefix: [*:0]wchar,
exec_prefix: [*:0]wchar,
base_exec_prefix: [*:0]wchar,

// --- Py_Main() ---
skip_source_first_line: c_int,
run_command: [*:0]wchar,
run_module: [*:0]wchar,
run_filename: [*:0]wchar,

_install_importlib: c_int,
_init_main: c_int,
_isolated_interpreter: c_int,
};

pub const PyWideStringList = extern struct {
length: isize,
items: [*][*]wchar,
};

pub const PyPreConfig = extern struct {
_config_init: c_int,
parse_argv: c_int,
isolated: c_int,
use_environment: c_int,
configure_locale: c_int,
coerce_c_locale: c_int,
coerce_c_locale_warn: c_int,
utf8_mode: c_int,
dev_mode: c_int,
allocator: c_int,
};

pub const PyStatus = extern struct {
exitcode: c_int,
err_msg: [*:0]const u8,
func: [*:0]const u8,
};

pub extern fn Py_PreInitialize(*const PyPreConfig) PyStatus;
pub extern fn PyPreConfig_InitPythonConfig(*PyPreConfig) void;
pub extern fn PyStatus_Exception(PyStatus) bool;
pub extern fn Py_ExitStatusException(PyStatus) noreturn;

pub extern fn Py_Initialize() void;
pub extern fn Py_Finalize() void;

pub extern fn PySys_SetPath([*:0]const wchar) void;

pub extern fn Py_DecRef(?*anyopaque) void;

pub extern fn Py_DecodeLocale([*:0]const u8, *usize) ?[*:0]u8;
pub extern fn PyConfig_SetBytesString(*PyConfig, *const [*:0]wchar, [*:0]const u8) PyStatus;
pub extern fn Py_SetProgramName([*:0]const u8) void;

pub extern fn Py_CompileString([*:0]const u8, [*:0]const u8, c_int) ?*anyopaque;
pub extern fn PyMarshal_WriteObjectToString(?*anyopaque, c_int) ?*anyopaque;
pub extern fn PyBytes_Size(?*anyopaque) usize;
pub extern fn PyBytes_AsString(?*anyopaque) ?[*:0]u8;

pub extern fn PyErr_Print() void;
pub extern fn PyErr_Fetch(?*anyopaque, ?*anyopaque, ?*anyopaque) void;
pub extern fn PyErr_NormalizeException(?*anyopaque, ?*anyopaque, ?*anyopaque) void;

pub extern fn PyConfig_InitPythonConfig(*PyConfig) void;
pub extern fn PyConfig_Clear(*PyConfig) void;
pub extern fn PyConfig_Read(*PyConfig) PyStatus;
pub extern fn Py_InitializeFromConfig(*PyConfig) PyStatus;

pub extern fn PyWideStringList_Append(*PyWideStringList, [*:0]const u32) PyStatus;

pub fn Sys_SetPath(path: [:0]const u16) void {
PySys_SetPath(path.ptr);
}

pub fn SetProgramName(name: [:0]const u8) void {
Py_SetProgramName(name.ptr);
}

pub fn CompileString(source: [:0]const u8, filename: [:0]const u8) ?*anyopaque {
return Py_CompileString(source.ptr, filename.ptr, Py_file_input);
}

pub fn Marshal_WriteObjectToString(code: ?*anyopaque) ?*anyopaque {
return PyMarshal_WriteObjectToString(code, Py_MARSHAL_VERSION);
}

pub fn Bytes_Size(code: ?*anyopaque) usize {
return PyBytes_Size(code);
}

pub fn Bytes_AsString(code: ?*anyopaque) ?[*:0]u8 {
return PyBytes_AsString(code);
}

pub fn PrintError() void {
PyErr_Print();

// TODO: fetch and normalize here
}
7 changes: 0 additions & 7 deletions src/compiler/Marshal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const ObjType = @import("objtype.zig").ObjType;
const CodeObject = @import("CodeObject.zig");
const Object = @import("../vm/Object.zig");
const Vm = @import("../vm/Vm.zig");
const tracer = @import("tracer");
const BigIntManaged = std.math.big.int.Managed;

const Marshal = @This();
Expand Down Expand Up @@ -90,9 +89,6 @@ fn createObject(marshal: *Marshal, comptime tag: Object.Tag, data: anytype) !*co
}

pub fn parse(marshal: *Marshal) !*const CodeObject {
const t = tracer.trace(@src(), "", .{});
defer t.end();

var co_obj = try marshal.readObject();
const co = co_obj.get(.codeobject);
return &co.value;
Expand Down Expand Up @@ -139,9 +135,6 @@ fn readSingleString(marshal: *Marshal) ![]const u8 {
}

fn readObject(marshal: *Marshal) Error!*const Object {
const t = tracer.trace(@src(), "", .{});
defer t.end();

const allocator = marshal.allocator;
var next_byte = marshal.bytes[marshal.cursor];
marshal.cursor += 1;
Expand Down
37 changes: 15 additions & 22 deletions src/frontend/Python.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ pub fn parse(
filename: [:0]const u8,
allocator: std.mem.Allocator,
) ![]const u8 {
const t = tracer.trace(@src(), "", .{});
defer t.end();

try Initialize(allocator);

const compiled = helpers.CompileString(source, filename);
const compiled = bindings.CompileString(source, filename);
if (null == compiled) {
helpers.PrintError();
bindings.PrintError();
std.posix.exit(1);
}

const bytecode = helpers.Marshal_WriteObjectToString(compiled);
const bytecode = bindings.Marshal_WriteObjectToString(compiled);
if (null == bytecode) {
return error.FailedToWriteObjectToString;
}

const size = helpers.Bytes_Size(bytecode);
const ptr = helpers.Bytes_AsString(bytecode);
const size = bindings.Bytes_Size(bytecode);
const ptr = bindings.Bytes_AsString(bytecode);
if (null == ptr) {
return error.FailedToAsStringCode;
}
Expand All @@ -52,8 +49,8 @@ pub fn parse(
try writer.writeInt(u32, @intCast(source.len), .little);
try writer.writeAll(pyc_bytes);

externs.Py_DecRef(bytecode);
externs.Py_Finalize();
bindings.Py_DecRef(bytecode);
bindings.Py_Finalize();

return bytes;
}
Expand All @@ -72,19 +69,19 @@ pub fn utf8ToUtf32Z(
pub fn Initialize(
allocator: std.mem.Allocator,
) !void {
var config: types.PyConfig = undefined;
externs.PyConfig_InitPythonConfig(&config);
defer externs.PyConfig_Clear(&config);
var config: bindings.PyConfig = undefined;
bindings.PyConfig_InitPythonConfig(&config);
defer bindings.PyConfig_Clear(&config);

// mute some silly errors that probably do infact matter
config.pathconfig_warnings = 0;

_ = externs.PyConfig_SetBytesString(
_ = bindings.PyConfig_SetBytesString(
&config,
&config.program_name,
"osmium".ptr,
);
_ = externs.PyConfig_Read(&config);
_ = bindings.PyConfig_Read(&config);

const utf32_path = try utf8ToUtf32Z(
build_options.lib_path,
Expand All @@ -93,12 +90,12 @@ pub fn Initialize(
defer allocator.free(utf32_path);

config.module_search_paths_set = 1;
_ = externs.PyWideStringList_Append(
_ = bindings.PyWideStringList_Append(
&config.module_search_paths,
utf32_path.ptr,
);

const status = externs.Py_InitializeFromConfig(&config);
const status = bindings.Py_InitializeFromConfig(&config);
// needs to be a pointer discard because the stack protector gets overrun?
_ = &status;
}
Expand All @@ -110,10 +107,6 @@ const std = @import("std");

const log = std.log.scoped(.python);

const cpython = @import("cpython");
const types = cpython.types;
const helpers = cpython.helpers;
const externs = cpython.externs;
const bindings = @import("../bindings.zig");

const tracer = @import("tracer");
const build_options = @import("options");
Loading

0 comments on commit cf7d1ea

Please sign in to comment.