Skip to content

Commit

Permalink
stop comptime interpreter from always crashing
Browse files Browse the repository at this point in the history
Don't worry, it is still going to crash easily (just like ZLS in general)
  • Loading branch information
Techatrix committed Nov 10, 2023
1 parent 5812238 commit 6edcc92
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
32 changes: 21 additions & 11 deletions src/ComptimeInterpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ ip: *InternPool,
document_store: *DocumentStore,
uri: DocumentStore.Uri,
namespaces: std.MultiArrayList(Namespace) = .{},
has_analyzed_root: bool = false,
mutex: std.Thread.Mutex = .{},

pub fn getHandle(interpreter: *ComptimeInterpreter) *DocumentStore.Handle {
// This interpreter is loaded from a known-valid handle so a valid handle must exist
Expand Down Expand Up @@ -892,18 +894,26 @@ pub fn interpret(
const import_handle = interpreter.document_store.getOrLoadHandle(import_uri) orelse return error.ImportFailure;
const import_interpreter = try import_handle.getComptimeInterpreter(interpreter.document_store, interpreter.ip);

return import_interpreter.interpret(0, .none, options) catch |err| {
log.err("Failed to interpret node: {s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
if (import_interpreter.mutex.tryLock()) {
defer import_interpreter.mutex.unlock();

if (!import_interpreter.has_analyzed_root) {
interpreter.has_analyzed_root = true;
_ = import_interpreter.interpret(0, .none, .{}) catch |err| {
log.err("Failed to interpret file: {s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
};
}
return InterpretResult{
.value = Value{
.interpreter = import_interpreter,
.node_idx = 0,
.index = .unknown_type,
},
};
}

return InterpretResult{
.value = Value{
.interpreter = import_interpreter,
.node_idx = 0,
.index = .unknown_type,
},
};
}

Expand Down
20 changes: 13 additions & 7 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1193,13 +1193,19 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
log.info("Invoking interpreter!", .{});

const interpreter = try handle.getComptimeInterpreter(analyser.store, analyser.ip);
_ = interpreter.interpret(0, .none, .{}) catch |err| {
log.err("Failed to interpret file: {s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
return null;
};
interpreter.mutex.lock();
defer interpreter.mutex.unlock();

if (!interpreter.has_analyzed_root) {
interpreter.has_analyzed_root = true;
_ = interpreter.interpret(0, .none, .{}) catch |err| {
log.err("Failed to interpret file: {s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
return null;
};
}

const root_namespace: ComptimeInterpreter.Namespace.Index = @enumFromInt(0);

Expand Down

0 comments on commit 6edcc92

Please sign in to comment.