forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_api.rs
More file actions
447 lines (395 loc) · 13.8 KB
/
Copy pathc_api.rs
File metadata and controls
447 lines (395 loc) · 13.8 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Stable C API for embedding the Nulang runtime.
//!
//! This module exposes a minimal, ABI-stable boundary so that C (or any other
//! language that can call C) can create a runtime, compile Nulang source,
//! execute it, and read the results.
//!
//! All public functions are `#[no_mangle] extern "C"`. The `NulangRuntime`
//! and `NulangValue` types are `#[repr(C)]` and can be passed by pointer or
//! by value across the FFI boundary.
use std::ffi::{c_char, c_void, CStr, CString};
use crate::effect_checker::{CapContext, CapabilityAnalyzer, EffectChecker, EffectContext};
use crate::lexer::Lexer;
use crate::parser::Parser;
use crate::typechecker::TypeChecker;
use crate::types::NuError;
use crate::vm::{Value, VM};
// ---------------------------------------------------------------------------
// Opaque runtime handle
// ---------------------------------------------------------------------------
/// An opaque runtime context that owns compiled modules and error state.
#[repr(C)]
pub struct NulangRuntime {
modules: Vec<crate::bytecode::CodeModule>,
last_error: Option<String>,
/// Holds the CString backing `nulang_last_error`.
error_cstring: Option<CString>,
/// Holds CStrings returned by `nulang_value_to_string`.
string_cache: Vec<CString>,
}
impl NulangRuntime {
fn new() -> Self {
NulangRuntime {
modules: Vec::new(),
last_error: None,
error_cstring: None,
string_cache: Vec::new(),
}
}
fn set_error(&mut self, err: NuError) {
self.last_error = Some(err.to_string());
}
fn clear_error(&mut self) {
self.last_error = None;
self.error_cstring = None;
}
fn compile(&mut self, source: &str) -> Option<usize> {
self.clear_error();
match compile_source(source) {
Ok(module) => {
let handle = self.modules.len();
self.modules.push(module);
Some(handle)
}
Err(e) => {
self.set_error(e);
None
}
}
}
fn run(&mut self, module_handle: usize) -> Option<Value> {
self.clear_error();
let module = self.modules.get(module_handle)?.clone();
let mut vm = VM::new();
vm.load_module(module);
match vm.run() {
Ok(value) => Some(value),
Err(e) => {
self.set_error(e);
None
}
}
}
fn last_error_ptr(&mut self) -> *const c_char {
match &self.last_error {
Some(msg) => {
let cstr = CString::new(msg.clone()).unwrap_or_else(|_| {
// The message should never contain interior nuls in practice.
CString::new("<invalid error message>").unwrap_or(CString::new("").unwrap())
});
let ptr = cstr.as_ptr();
self.error_cstring = Some(cstr);
ptr
}
None => std::ptr::null(),
}
}
fn value_to_cached_cstr(&mut self, value: Value) -> *const c_char {
let text = value.to_string_repr();
let cstr = CString::new(text).unwrap_or_else(|_| CString::new("").unwrap());
let ptr = cstr.as_ptr();
self.string_cache.push(cstr);
ptr
}
}
// ---------------------------------------------------------------------------
// Compilation pipeline
// ---------------------------------------------------------------------------
fn compile_source(source: &str) -> Result<crate::bytecode::CodeModule, NuError> {
let mut lexer = Lexer::new(source);
let tokens = lexer.lex()?;
let mut parser = Parser::new(tokens);
let ast = parser.parse_module()?;
let mut type_checker = TypeChecker::new();
let _module_type = type_checker.check_module(&ast)?;
let mut effect_checker = EffectChecker::new();
let effect_ctx = EffectContext::empty();
for decl in &ast.decls {
if let crate::ast::Decl::Function { body, .. } = decl {
effect_checker.infer_effects(&effect_ctx, body)?;
}
}
let mut cap_analyzer = CapabilityAnalyzer::new();
let cap_ctx = CapContext::new();
for decl in &ast.decls {
if let crate::ast::Decl::Function { body, .. } = decl {
cap_analyzer.infer_cap(&cap_ctx, body)?;
}
}
let hir = crate::hir_lower::lower_module(&ast);
let mir = crate::mir_lower::lower_module(&hir)?;
let code_module = crate::mir_codegen::compile_mir(&mir, "main")?;
Ok(code_module)
}
// ---------------------------------------------------------------------------
// C value type
// ---------------------------------------------------------------------------
/// A Nulang value exposed to C.
///
/// Internally this is just the raw NaN-boxed bits. Use the extractor
/// functions below to read primitive data out of it.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct NulangValue {
raw: u64,
}
impl From<Value> for NulangValue {
fn from(value: Value) -> Self {
NulangValue {
raw: value.to_bits(),
}
}
}
impl From<NulangValue> for Value {
fn from(value: NulangValue) -> Self {
Value::from_bits(value.raw)
}
}
// ---------------------------------------------------------------------------
// C API functions
// ---------------------------------------------------------------------------
/// Create a new Nulang runtime.
#[no_mangle]
pub extern "C" fn nulang_runtime_new() -> *mut NulangRuntime {
let runtime = Box::new(NulangRuntime::new());
Box::into_raw(runtime)
}
/// Free a Nulang runtime created by `nulang_runtime_new`.
///
/// # Safety
/// `runtime` must be a pointer returned by `nulang_runtime_new` and must not
/// be used after this call.
#[no_mangle]
pub unsafe extern "C" fn nulang_runtime_free(runtime: *mut NulangRuntime) {
if !runtime.is_null() {
// SAFETY: caller guarantees the pointer came from `nulang_runtime_new`.
unsafe {
let _ = Box::from_raw(runtime);
}
}
}
/// Compile Nulang source code.
///
/// Returns a non-negative module handle on success, or -1 on error.
/// On error, the error message can be retrieved with `nulang_last_error`.
///
/// # Safety
/// `source` must be a valid, null-terminated UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn nulang_compile(runtime: *mut NulangRuntime, source: *const c_char) -> i64 {
if runtime.is_null() || source.is_null() {
return -1;
}
// SAFETY: caller guarantees `source` is a valid C string.
let source_str = unsafe {
match CStr::from_ptr(source).to_str() {
Ok(s) => s,
Err(_) => return -1,
}
};
// SAFETY: runtime is non-null and valid.
let rt = unsafe { &mut *runtime };
match rt.compile(source_str) {
Some(handle) => handle as i64,
None => -1,
}
}
/// Run a previously compiled module.
///
/// Returns the resulting value. If execution failed, the result is `nil` and
/// `nulang_last_error` will return the error message.
///
/// # Safety
/// `runtime` must be a valid pointer returned by `nulang_runtime_new`.
#[no_mangle]
pub unsafe extern "C" fn nulang_run(
runtime: *mut NulangRuntime,
module_handle: i64,
) -> NulangValue {
if runtime.is_null() || module_handle < 0 {
return Value::nil().into();
}
// SAFETY: runtime is non-null and valid.
let rt = unsafe { &mut *runtime };
match rt.run(module_handle as usize) {
Some(value) => value.into(),
None => Value::nil().into(),
}
}
/// Return the last error message, or `NULL` if there is none.
///
/// The returned pointer is owned by the runtime and remains valid until the
/// next call that modifies the error state or until the runtime is freed.
///
/// # Safety
/// `runtime` must be a valid pointer returned by `nulang_runtime_new`.
#[no_mangle]
pub unsafe extern "C" fn nulang_last_error(runtime: *mut NulangRuntime) -> *const c_char {
if runtime.is_null() {
return std::ptr::null();
}
// SAFETY: runtime is non-null and valid.
let rt = unsafe { &mut *runtime };
rt.last_error_ptr()
}
/// Extract an integer from a Nulang value.
///
/// Returns 0 if the value is not an integer.
#[no_mangle]
pub extern "C" fn nulang_value_int(value: NulangValue) -> i64 {
Value::from(value).as_int().unwrap_or(0)
}
/// Extract a float from a Nulang value.
///
/// Returns 0.0 if the value is not a float.
#[no_mangle]
pub extern "C" fn nulang_value_float(value: NulangValue) -> f64 {
Value::from(value).as_float().unwrap_or(0.0)
}
/// Extract a boolean from a Nulang value.
///
/// Returns `false` if the value is not a boolean.
#[no_mangle]
pub extern "C" fn nulang_value_bool(value: NulangValue) -> bool {
Value::from(value).as_bool().unwrap_or(false)
}
/// Check whether a value is `nil`.
#[no_mangle]
pub extern "C" fn nulang_value_is_nil(value: NulangValue) -> bool {
Value::from(value).is_nil()
}
/// Check whether a value is the unit value `()`.
#[no_mangle]
pub extern "C" fn nulang_value_is_unit(value: NulangValue) -> bool {
Value::from(value).is_unit()
}
/// Return a C string representation of a Nulang value.
///
/// The returned pointer is owned by the runtime and remains valid until the
/// runtime is freed. The caller must not free it.
///
/// # Safety
/// `runtime` must be a valid pointer returned by `nulang_runtime_new`.
#[no_mangle]
pub unsafe extern "C" fn nulang_value_to_string(
runtime: *mut NulangRuntime,
value: NulangValue,
) -> *const c_char {
if runtime.is_null() {
return std::ptr::null();
}
// SAFETY: runtime is non-null and valid.
let rt = unsafe { &mut *runtime };
rt.value_to_cached_cstr(Value::from(value))
}
/// Register a native C function so it can be called from Nulang.
///
/// The function pointer must match the supplied parameter and return types.
/// Use `"__nulang_registered__"` as the sentinel library name in the Nulang
/// `extern` block when no dynamic library is required.
///
/// `params` is a pointer to an array of `CType` values; `param_count` is the
/// array length. `ret` is the return type. The pointer and array are only
/// borrowed for the duration of the call.
///
/// Returns 0 on success, -1 on error.
///
/// # Safety
/// `name` must be a valid null-terminated UTF-8 string. `ptr` must point to a
/// valid function whose C ABI matches the given types. If `param_count` is
/// non-zero, `params` must point to at least `param_count` valid `CType`
/// values.
#[no_mangle]
pub unsafe extern "C" fn nulang_register_native_function(
name: *const c_char,
ptr: *const c_void,
params: *const super::marshal::CType,
param_count: usize,
ret: super::marshal::CType,
) -> i32 {
if name.is_null() || ptr.is_null() {
return -1;
}
// SAFETY: caller guarantees `name` is a valid C string.
let name_str = unsafe {
match CStr::from_ptr(name).to_str() {
Ok(s) => s,
Err(_) => return -1,
}
};
let params_slice = if param_count == 0 {
&[]
} else if params.is_null() {
return -1;
} else {
// SAFETY: caller guarantees `params` points to `param_count` valid values.
unsafe { std::slice::from_raw_parts(params, param_count) }
};
let signature = super::marshal::Signature::new(params_slice.to_vec(), ret);
// SAFETY: caller guarantees `ptr` matches the signature.
unsafe {
match super::native::register_native_function(name_str, ptr, signature) {
Ok(()) => 0,
Err(_) => -1,
}
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CString;
#[test]
fn test_c_api_compile_and_run() {
let rt = nulang_runtime_new();
assert!(!rt.is_null());
let source = CString::new("1 + 2").unwrap();
// SAFETY: rt is valid and source is a valid C string.
let handle = unsafe { nulang_compile(rt, source.as_ptr()) };
assert!(handle >= 0, "compile failed");
// SAFETY: rt is valid and handle is valid.
let value = unsafe { nulang_run(rt, handle) };
assert_eq!(nulang_value_int(value), 3);
// SAFETY: rt is valid.
unsafe { nulang_runtime_free(rt) };
}
#[test]
fn test_c_api_compile_error() {
let rt = nulang_runtime_new();
let source = CString::new("let x = in").unwrap();
// SAFETY: rt is valid and source is a valid C string.
let handle = unsafe { nulang_compile(rt, source.as_ptr()) };
assert_eq!(handle, -1);
// SAFETY: rt is valid.
let err = unsafe { nulang_last_error(rt) };
assert!(!err.is_null());
// SAFETY: rt is valid.
unsafe { nulang_runtime_free(rt) };
}
#[test]
fn test_c_api_value_extractors() {
let int_val: NulangValue = Value::int(42).into();
assert_eq!(nulang_value_int(int_val), 42);
let float_val: NulangValue = Value::float(2.5).into();
assert!((nulang_value_float(float_val) - 2.5).abs() < f64::EPSILON);
let bool_val: NulangValue = Value::bool(true).into();
assert!(nulang_value_bool(bool_val));
assert!(nulang_value_is_nil(Value::nil().into()));
assert!(nulang_value_is_unit(Value::unit().into()));
}
#[test]
fn test_c_api_value_to_string() {
let rt = nulang_runtime_new();
let value: NulangValue = Value::int(123).into();
// SAFETY: rt is valid.
let ptr = unsafe { nulang_value_to_string(rt, value) };
assert!(!ptr.is_null());
// SAFETY: ptr points to a valid CString owned by the runtime.
let s = unsafe { CStr::from_ptr(ptr).to_str().unwrap() };
assert_eq!(s, "123");
// SAFETY: rt is valid.
unsafe { nulang_runtime_free(rt) };
}
}