summaryrefslogtreecommitdiff
path: root/src/Interpreter.zig
blob: 68627060d265d012fef71dee7bfb9dd8228b44de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const c = @import("c");

const zpy = @import("root.zig");

const Self = @This();

globals: zpy.Dict,

pub fn init() !Self {
    c.Py_Initialize();

    const self: Self = .{
        .globals = try .new(),
    };

    return self;
}

pub const RunOptions = struct {
    locals: ?*zpy.Dict = null,
};

pub fn run(self: *Self, str: [*c]const u8, options: RunOptions) zpy.Error!zpy.Object {
    if (c.PyRun_String(
            str,
            c.Py_file_input,
            self.globals.object.ptr,
            if (options.locals) |loc| loc.object.ptr else null,
    )) |object| {
        return .fromPtr(object);
    }

    return error.Exception;
}

pub fn deinit(self: *Self) void {
    c.Py_Finalize();
    self.* = undefined;
}