Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+704
View File
@@ -0,0 +1,704 @@
#! @file jobs.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains how the server has a job worked out: by asking the base, which
#! starts a copy of itself for the job, or where the base cannot, by a process
#! of the server's own that works it out from nothing. Either way the job runs
#! in a process of its own, which cannot take the server down with it, and is
#! stopped where it takes too long.
#!
#! Windows cannot start a copy of a process, so there the server's processes
#! are this program started again, `lsp --base` and `lsp --worker`, which read
#! what the server works with from their input. The base answers each job
#! itself, bringing the program up to date with the files as they are, so a job
#! that faults or never ends takes only the base with it, which the server stops
#! and makes again. A worker works out one job from nothing, as the process of
#! the server's own does on Linux.
import std::process;
import system;
import sakura::analyzer;
module sakura::server;
#! What each kind of job is doing, as the client's log says where it fails.
#! @param kind The kind of job.
#! @returns What it is doing.
def doing(kind: task): strview {
return match kind {
task::check => "Checking the workspace",
task::hover => "Describing the name hovered",
task::complete => "Completing the name",
task::call => "Describing what is called",
task::tokens => "Colouring the file",
task::name => "Finding what the name refers to",
task::refer => "Finding where the name is written"
};
}
#! Writes the documents open in the editor to a handle: how many there are, then
#! the URI, path, and text of each.
#! @param handle The handle.
#! @param documents The documents.
#! @returns Whether they were written.
def put_documents(handle: system::fd, documents: &vector<document>): bool {
var sent := put_size(handle, documents.length);
for &each in documents {
sent = sent && put_text(handle, each.uri.span) && put_text(handle, each.path.span)
&& put_text(handle, each.text.span);
}
return sent;
}
#! Reads the documents open in the editor from a handle, as `put_documents`
#! writes them, after those already held.
#! @param handle The handle.
#! @param documents Where the documents are appended.
#! @returns Whether they were read.
def get_documents(handle: system::fd, documents: &mut vector<document>): bool {
var count: u64 = 0;
return false if !get_size(handle, &mut count, 0);
for _ in 0..count {
var each := document { uri = string::new(nothing {}), path = string::new(nothing {}), text = string::new(nothing {}) };
return false if !get_text(handle, &mut each.uri, 0) || !get_text(handle, &mut each.path, 0)
|| !get_text(handle, &mut each.text, 0);
documents.append(move(&mut each));
}
return true;
}
#! Writes a job to a handle, with the documents open in the editor.
#! @param handle The handle.
#! @param asked The job.
#! @param documents The documents.
#! @returns Whether it was written.
def put_job(handle: system::fd, asked: &job, documents: &vector<document>): bool {
return put_size(handle, asked.task as u64) && put_size(handle, asked.strict ? 1 : 0)
&& put_size(handle, asked.line as u64) && put_size(handle, asked.character as u64)
&& put_text(handle, asked.path.span) && put_text(handle, asked.touched.span)
&& put_text(handle, asked.entry.span) && put_documents(handle, documents);
}
#! Reads a job from a handle, as `put_job` writes it, with the documents open in
#! the editor, which replace those known before.
#! @param handle The handle.
#! @param asked Where the job is written.
#! @param documents Where the documents are written.
#! @returns Whether it was read.
def get_job(handle: system::fd, asked: &mut job, documents: &mut vector<document>): bool {
var kind: u64 = 0;
var strict: u64 = 0;
var line: u64 = 0;
var character: u64 = 0;
val read := get_size(handle, &mut kind, 0) && get_size(handle, &mut strict, 0)
&& get_size(handle, &mut line, 0) && get_size(handle, &mut character, 0)
&& get_text(handle, &mut asked.path, 0) && get_text(handle, &mut asked.touched, 0)
&& get_text(handle, &mut asked.entry, 0);
return false if !read || kind > task::refer as u64;
asked.task = kind as u8 as task;
asked.strict = strict != 0;
asked.line = line as u32;
asked.character = character as u32;
documents.clear();
return get_documents(handle, documents);
}
#! A job with nothing in it, to be read into.
#! @returns The job.
def empty_job(): job {
return job {
task = task::check,
path = string::new(nothing {}),
touched = string::new(nothing {}),
entry = string::new(nothing {})
};
}
#! Does a job, asking the base and working it out from nothing where the base
#! cannot, and gives whether it was done. A job about a file touches it, so one
#! about a file declaring `main` is answered from that file's program.
#! @param this The server.
#! @param asked The job.
#! @param output Where the answer is written.
#! @returns Whether it was done.
def server::run_job(&mut this, asked: &mut job, output: &mut string): bool {
this.touch(asked.path.span);
asked.touched = string::copy_of(&this.touched);
asked.entry = string::copy_of(&this.entry);
if !this.ask(asked, output) {
output.clear();
return false if !this.fork_job(asked, output);
}
this.answered(asked, output);
return true;
}
#! Takes the file declaring `main` that a job's program began with off the
#! front of its answer, and keeps it, so the next job is answered from the same
#! program unless another such file is touched. A job answered from another
#! program than the last says so, and has the workspace checked again where it
#! was not a check, so the diagnostics shown follow it.
#! @param this The server.
#! @param asked The job.
#! @param output The answer, which the file is taken from.
def server::answered(&mut this, asked: &job, output: &mut string): void {
val zero: u8 = 0;
val end := output.span.index_of(&zero) else { return; };
val chosen := output.substr(0, end);
if chosen != this.entry.span {
val moved := this.entry.length != 0 && chosen.length != 0;
this.entry = string::from(chosen, nothing {});
this.dirty = this.dirty || asked.task != task::check;
if moved {
val shown := this.shown_path(this.entry.span);
var text := string::from("Checking the program begun by ", nothing {});
text.append(shown);
text.append(". The other files declaring `main` are left out until one of them is opened, edited, or asked about.");
this.log(log_info, text.span);
}
}
var rest := string::from(output.substr(end + 1, output.length - end - 1), nothing {});
output = move(&mut rest);
}
#! Asks the base for a job, giving false where it could not answer, which leaves
#! the job to be worked out from nothing. A base whose files moved on is made
#! again, and the job asked of the new one. A strict check leaves out files the
#! base checked, so it is always worked out from nothing, and
#! `SAKURA_LSP_BASE=0` asks for every job to be, which is how the two are
#! compared.
#! @param this The server.
#! @param asked The job.
#! @param output Where the answer is written.
#! @returns Whether the base answered.
def server::ask(&mut this, asked: &job, output: &mut string): bool {
return false if asked.strict;
if variable("SAKURA_LSP_BASE") is some(wanted) {
return false if wanted == "0";
}
var status := this.ask_once(asked, output);
if status == answer_stale {
this.stop_base();
output.clear();
status = this.ask_once(asked, output);
}
return status == answer_ok;
}
#! Asks the base for a job once.
#! @param this The server.
#! @param asked The job.
#! @param output Where the answer is written.
#! @returns What the base said of it, or `answer_fail` where it did not answer.
def server::ask_once(&mut this, asked: &job, output: &mut string): u64 {
return answer_fail if !this.start_base();
val sent := put_job(this.base.jobs, asked, &this.documents);
var status: u64 = answer_fail;
var answer := string::new(nothing {});
val deadline := now_ms() + timeout;
val heard := sent && get_size(this.base.answers, &mut status, deadline)
&& get_text(this.base.answers, &mut answer, deadline);
if !heard {
this.stop_base();
if sent {
this.log_failure(asked.task, " took longer than 30 seconds, or stopped unexpectedly, and was stopped.");
}
return answer_fail;
}
if status == answer_ok {
output.append(answer.span);
}
return status;
}
#! Logs that a job was not done: what it was doing, then what went wrong.
#! @param this The server.
#! @param kind The kind of job.
#! @param what What went wrong, which follows what it was doing, as
#! ` stopped unexpectedly.`.
def server::log_failure(&mut this, kind: task, what: strview): void {
var text := string::from(doing(kind), nothing {});
text.append(what);
this.log(log_error, text.span);
}
#! The base itself: it reads and checks the files of the workspace once, then
#! answers each job, as `answer_jobs` does on the system it runs on.
#! @param this The server, as the base copied or read it.
#! @param jobs Where jobs are read.
#! @param answers Where answers are written.
def server::base_main(&mut this, jobs: system::fd, answers: system::fd): void {
# On Linux, the base copies the server, so it leads a group of its own, which
# the copies it starts belong to, and writes nothing where the server answers.
when os == linux {
lead_group();
quiet_output();
}
var files := gather_all(&this.gathering());
var held := program::parse(&files, false, this.options.native, this.options.system, &this.gathering());
var checker := checker::new(&mut held.names, &mut held.check_notes);
held.begin(&mut checker, held.choose(this.touched.span, this.entry.span));
checker.check();
this.answer_jobs(&mut held, &mut checker, jobs, answers);
}
#! Gathers the files a job is worked out from as they are now, with the
#! placeholder written where it completes a name after `.`.
#! @param this The server.
#! @param asked The job.
#! @returns The files.
def server::gather_for(&this, asked: &job): vector<gathered> {
var files := gather_all(&this.gathering());
write_placeholder(&mut files, asked);
return files;
}
#! Works out one job from nothing, in a process of its own: reads the files,
#! parses and checks them, answers, and writes the answer.
#! @param this The server, as the process copied or read it.
#! @param asked The job.
#! @param out Where the answer is written.
def server::work_alone(&mut this, asked: &job, out: system::fd): void {
var files := this.gather_for(asked);
var held := program::parse(&files, asked.strict, this.options.native, this.options.system, &this.gathering());
var checker := checker::new(&mut held.names, &mut held.check_notes);
held.begin(&mut checker, held.choose(asked.touched.span, asked.entry.span));
if asked.task != task::check {
checker.check();
}
var place := workplace { held = &mut held, checker = &mut checker, documents = &this.documents };
var made := string::new(nothing {});
answer(&mut place, asked, &mut made);
put_all(out, made.span);
}
#! The two pipes between the server and a process of its own: one jobs are sent
#! to it by, and one its answers come back by.
struct pipes {
var jobs_read: system::fd; #! Where the process reads jobs.
var jobs_write: system::fd; #! Where the server writes them.
var answers_read: system::fd; #! Where the server reads answers.
var answers_write: system::fd; #! Where the process writes them.
}
#! Makes the two pipes between the server and a process of its own, or neither.
#! @returns The pipes, or nothing where they could not be made.
def make_pipes(): maybe<pipes> {
var made := pipes { jobs_read = -1, jobs_write = -1, answers_read = -1, answers_write = -1 };
return nothing {} if system::pipe(&mut made.jobs_read, &mut made.jobs_write) != 0;
if system::pipe(&mut made.answers_read, &mut made.answers_write) != 0 {
made.jobs_read.close();
made.jobs_write.close();
return nothing {};
}
return made;
}
#! Closes the ends of the pipes the process keeps, which the server lets go of
#! once the process has them.
#! @param this The pipes.
def pipes::close_process_ends(&this): void {
this.jobs_read.close();
this.answers_write.close();
}
#! Closes the ends of the pipes the server keeps.
#! @param this The pipes.
def pipes::close_server_ends(&this): void {
this.jobs_write.close();
this.answers_read.close();
}
when os == linux {
#! Starts the base, giving false where it could not be, so the job is worked out
#! from nothing instead.
#! @param this The server.
#! @returns Whether the base is running.
def server::start_base(&mut this): bool {
return true if this.base.process > 0;
val ends := make_pipes() else { return false; };
val child := system::fork();
if child == 0 {
ends.close_server_ends();
this.base_main(ends.jobs_read, ends.answers_write);
system::exit(0);
}
ends.close_process_ends();
if child < 0 {
ends.close_server_ends();
return false;
}
this.base = base_link { process = child, jobs = ends.jobs_write, answers = ends.answers_read };
return true;
}
#! Stops the base, and every copy of it working out a job, which is made again
#! once it is next needed.
#! @param this The server.
def server::stop_base(&mut this): void {
return if this.base.process <= 0;
this.base.jobs.close();
this.base.answers.close();
system::kill(0 - this.base.process, system::signal::kill);
var status := system::process_status {};
reap(this.base.process, &mut status);
this.base = base_link {};
}
#! Answers each job the base is sent by a copy of itself, which brings the
#! program up to date with the files as they are and answers, and once a check
#! is answered, brings the base up to date the same way.
#! @param this The server, as the base copied it.
#! @param held The program, as the base read and checked it.
#! @param checker Its checker.
#! @param jobs Where jobs are read.
#! @param answers Where answers are written.
def server::answer_jobs(&mut this, held: &mut program, checker: &mut checker, jobs: system::fd, answers: system::fd): void {
var asked := empty_job();
while true {
if !get_job(jobs, &mut asked, &mut this.documents) {
system::exit(0);
}
var reading: system::fd = -1;
var writing: system::fd = -1;
if system::pipe(&mut reading, &mut writing) != 0 {
put_size(answers, answer_fail);
put_text(answers, "");
continue;
}
val worker := system::fork();
if worker == 0 {
reading.close();
jobs.close();
answers.close();
val code := this.base_work(held, checker, &asked, writing);
system::exit(code);
}
writing.close();
var output := string::new(nothing {});
if worker > 0 {
get_rest(reading, &mut output);
}
reading.close();
var status := system::process_status {};
val waited := worker > 0 && reap(worker, &mut status);
val done := waited && status.exited && status.code == 0;
val stale := waited && status.exited && status.code == worker_stale;
if !put_size(answers, done ? answer_ok : stale ? answer_stale : answer_fail)
|| !put_text(answers, done ? output.span : "") {
system::exit(0);
}
# The copy showed the files can be brought up to date without a fault, so
# once a check is answered, the base brings itself up to date too.
if done && asked.task == task::check {
var now := gather_all(&this.gathering());
if held.sync(checker, &now, asked.touched.span, asked.entry.span, true, &this.gathering()) == synced::stale {
system::exit(0);
}
}
}
}
#! Works out one job, in a copy of the base, which brings the program up to date
#! with the files as they are, answers, and writes the answer.
#! @param this The server, as the base copied it.
#! @param held The program, as the base has it.
#! @param checker Its checker.
#! @param asked The job.
#! @param out Where the answer is written.
#! @returns The status to exit with: 0 where the job is answered, and
#! `worker_stale` where the files moved on in a way the program cannot
#! follow.
def server::base_work(&mut this, held: &mut program, checker: &mut checker, asked: &job, out: system::fd): i32 {
var files := this.gather_for(asked);
# A check checks the program whole, so what changed is only parsed again.
val checks := asked.task != task::check;
return worker_stale if held.sync(checker, &files, asked.touched.span, asked.entry.span, checks, &this.gathering()) == synced::stale;
var place := workplace { held = held, checker = checker, documents = &this.documents };
var output := string::new(nothing {});
answer(&mut place, asked, &mut output);
put_all(out, output.span);
return 0;
}
#! Works out a job from nothing, in a process of its own, and collects what it
#! writes. Gives false where it did not finish.
#! @param this The server.
#! @param asked The job.
#! @param output Where the answer is written.
#! @returns Whether it finished.
def server::fork_job(&mut this, asked: &job, output: &mut string): bool {
var reading: system::fd = -1;
var writing: system::fd = -1;
if system::pipe(&mut reading, &mut writing) != 0 {
this.log_failure(asked.task, " failed: no pipe could be made.");
return false;
}
val child := system::fork();
if child == 0 {
reading.close();
quiet_output();
this.work_alone(asked, writing);
system::exit(0);
}
writing.close();
if child < 0 {
reading.close();
this.log_failure(asked.task, " failed: no process could be started.");
return false;
}
val finished := get_until(reading, output, now_ms() + timeout);
reading.close();
if !finished {
system::kill(child, system::signal::kill);
}
var status := system::process_status {};
reap(child, &mut status);
if !finished {
this.log_failure(asked.task, " took longer than 30 seconds and was stopped.");
} else if !status.exited {
val said := $" stopped unexpectedly with signal {status.signal}.";
this.log_failure(asked.task, said.span);
} else if status.code != 0 {
this.log_failure(asked.task, " stopped unexpectedly.");
} else {
return true;
}
return false;
}
}
when os == windows {
# The word a process the server starts is sent first, which changes whenever
# what it is sent does, so a process of another build, as one put in place of
# the executable while the server runs, says it cannot read what it is sent
# rather than misreading it.
val process_version: u64 = 3;
# The digits of hexadecimal, as the code of an exception is written with them,
# lowest first.
val hex_upper: strview = "0123456789ABCDEF";
#! Writes what a process the server starts works with, which on Windows it
#! cannot copy from the server: the libraries, the standard library's among
#! them, what the workspace is checked for, the file last touched and the
#! program last answered from, the workspace folders, and the documents open
#! in the editor.
#! @param this The server.
#! @param handle The handle.
#! @param documents The documents, which are none for a worker, since the
#! job it is sent brings them.
#! @returns Whether it was written.
def server::put_state(&this, handle: system::fd, documents: &vector<document>): bool {
var sent := put_size(handle, process_version)
&& put_size(handle, this.options.native ? 1 : 0) && put_text(handle, this.options.system)
&& put_text(handle, this.touched.span) && put_text(handle, this.entry.span)
&& put_size(handle, this.libraries.length);
for &each in this.libraries {
sent = sent && put_text(handle, each.span);
}
sent = sent && put_size(handle, this.roots.length);
for &each in this.roots {
sent = sent && put_text(handle, each.span);
}
return sent && put_documents(handle, documents);
}
#! Reads what a process the server starts works with, as `put_state` writes
#! it, after the word of its version, which `serve_process` reads.
#! @param this The process's server, which it is read into.
#! @param handle The handle.
#! @param system Where the name of the system the workspace is checked for
#! is kept, which the server's options view.
#! @returns Whether it was read.
def server::get_state(&mut this, handle: system::fd, system: &mut string): bool {
var native: u64 = 0;
var libraries: u64 = 0;
var roots: u64 = 0;
val read := get_size(handle, &mut native, 0)
&& get_text(handle, system, 0) && get_text(handle, &mut this.touched, 0)
&& get_text(handle, &mut this.entry, 0) && get_size(handle, &mut libraries, 0);
return false if !read;
this.options.native = native != 0;
this.options.system = system.span;
for _ in 0..libraries {
var library := string::new(nothing {});
return false if !get_text(handle, &mut library, 0);
this.libraries.append(move(&mut library));
}
return false if !get_size(handle, &mut roots, 0);
for _ in 0..roots {
var root := string::new(nothing {});
return false if !get_text(handle, &mut root, 0);
this.roots.append(move(&mut root));
}
return get_documents(handle, &mut this.documents);
}
#! Works out what the server asks of a process it started, which on Windows is
#! this program started again, as `lsp --base` or `lsp --worker`: it reads what
#! the server works with from its standard input, then answers, as the base,
#! each job it is sent there, or as a worker, the one job, writing the answers
#! to its standard output.
#! @param base Whether it is the base, rather than a worker.
#! @returns The status to exit with: 0 where it read what it was sent, 2 where
#! another build of the server sent it, and 1 otherwise.
pub def serve_process(base: bool): i32 {
val answers := take_output();
var version: u64 = 0;
return 1 if !get_size(system::fd::stdin, &mut version, 0);
if version != process_version {
system::fd::stderr.put("sakura: lsp: a process of the language server is only started by the server of the same build\n");
return 2;
}
var system := string::new(nothing {});
var running := make_server(&server_options {});
return 1 if !running.get_state(system::fd::stdin, &mut system);
if base {
running.base_main(system::fd::stdin, answers);
return 0;
}
var asked := empty_job();
return 1 if !get_job(system::fd::stdin, &mut asked, &mut running.documents);
running.work_alone(&asked, answers);
return 0;
}
#! Starts the base, giving false where it could not be, so the job is worked out
#! from nothing instead.
#! @param this The server.
#! @returns Whether the base is running.
def server::start_base(&mut this): bool {
return true if this.base.process > 0;
val ends := make_pipes() else { return false; };
val child := start_process("--base", ends.jobs_read, ends.answers_write);
ends.close_process_ends();
if child == 0 {
ends.close_server_ends();
return false;
}
this.base = base_link { process = child, jobs = ends.jobs_write, answers = ends.answers_read };
# The base reads what it works with before anything else, so it is sent now.
if !this.put_state(ends.jobs_write, &this.documents) {
this.stop_base();
return false;
}
return true;
}
#! Stops the base, which answers each job itself, so nothing else is working
#! one out, and which is made again once it is next needed.
#! @param this The server.
def server::stop_base(&mut this): void {
return if this.base.process <= 0;
this.base.jobs.close();
this.base.answers.close();
var code: u32 = 0;
end_process(this.base.process, 0, &mut code);
this.base = base_link {};
}
#! Answers each job the base is sent itself, bringing the program up to date
#! with the files as they are, since it cannot answer by a copy of itself.
#! Files that moved on in a way the program cannot follow end it once it has
#! said so, and the server makes another.
#! @param this The server, as the base read it.
#! @param held The program, as the base read and checked it.
#! @param checker Its checker.
#! @param jobs Where jobs are read.
#! @param answers Where answers are written.
def server::answer_jobs(&mut this, held: &mut program, checker: &mut checker, jobs: system::fd, answers: system::fd): void {
var asked := empty_job();
while true {
return if !get_job(jobs, &mut asked, &mut this.documents);
var output := string::new(nothing {});
val status := this.base_answer(held, checker, &asked, &mut output);
return if !put_size(answers, status) || !put_text(answers, output.span) || status != answer_ok;
}
}
#! Works out one job in the base itself, which brings the program up to date
#! with the files as they are, checking again what that affects, as the base
#! on Linux brings itself up to date once a check is answered, and answers.
#! @param this The server, as the base read it.
#! @param held The program.
#! @param checker Its checker.
#! @param asked The job.
#! @param out Where the answer is written.
#! @returns `answer_ok` where the job is answered, and `answer_stale` where the
#! files moved on in a way the program cannot follow, which leaves it
#! brought up to date only in part.
def server::base_answer(&mut this, held: &mut program, checker: &mut checker, asked: &job, out: &mut string): u64 {
var files := this.gather_for(asked);
return answer_stale if held.sync(checker, &files, asked.touched.span, asked.entry.span, true, &this.gathering()) == synced::stale;
var place := workplace { held = held, checker = checker, documents = &this.documents };
answer(&mut place, asked, out);
return answer_ok;
}
#! Works out a job from nothing, in a worker, a process of its own, and
#! collects what it writes. Gives false where it did not finish.
#! @param this The server.
#! @param asked The job.
#! @param output Where the answer is written.
#! @returns Whether it finished.
def server::fork_job(&mut this, asked: &job, output: &mut string): bool {
val ends := make_pipes() else {
this.log_failure(asked.task, " failed: no pipe could be made.");
return false;
};
val child := start_process("--worker", ends.jobs_read, ends.answers_write);
ends.close_process_ends();
if child == 0 {
ends.close_server_ends();
this.log_failure(asked.task, " failed: no process could be started.");
return false;
}
# The worker reads all it is sent before it writes anything, so it is sent
# all at once, and its answer read until it ends. The answer ends as the
# worker does, unless it closed its output and went on.
val none := vector::<document>::new();
if this.put_state(ends.jobs_write, &none) {
put_job(ends.jobs_write, asked, &this.documents);
}
ends.jobs_write.close();
val deadline := now_ms() + timeout;
val finished := get_until(ends.answers_read, output, deadline);
ends.answers_read.close();
var code: u32 = 1;
val ended := end_process(child, finished ? deadline : 0, &mut code);
if !ended {
this.log_failure(asked.task, " took longer than 30 seconds and was stopped.");
} else if code >= 0xC0000000 {
var said := string::from(" stopped unexpectedly with exception 0x", nothing {});
for i in 0u..8u {
said.push(hex_upper[((code >> (28 - 4 * i as u32)) & 0xF) as u64]);
}
said.append(".");
this.log_failure(asked.task, said.span);
} else if code != 0 {
val said := $" stopped unexpectedly with exit code {code}.";
this.log_failure(asked.task, said.span);
} else {
return true;
}
return false;
}
}