Skip to content

Commit

Permalink
Refactor: streamline evalWire function for clarity
Browse files Browse the repository at this point in the history
Refactor the evalWire function to enhance readability and maintainability by clearly organizing steps for AST reading, evaluation, environment creation, transformation, and cleanup. Ensure proper resource management and error handling throughout the function.
  • Loading branch information
sinkingsugar committed Dec 1, 2024
1 parent 9aee28a commit d21e916
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/shards/shards.h
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ struct SHLEvalEnv;
typedef struct SHLAst(__cdecl *SHReadProc)(struct SHStringWithLen name, struct SHStringWithLen code,
struct SHStringWithLen basePath, const struct SHStringWithLen *includeDirs,
uint32_t numIncludeDirs);
typedef struct SHLAst(__cdecl *SHLoadAstProc)(const uint8_t *bytes, uint32_t size);
typedef struct SHLEvalEnv *(__cdecl *SHCreateEvalEnv)(struct SHStringWithLen namespace_);
typedef void(__cdecl *SHFreeEvalEnv)(struct SHLEvalEnv *env);
typedef struct SHLError *(__cdecl *SHEvalProc)(struct SHLEvalEnv *env, const struct SHVar *ast);
Expand Down Expand Up @@ -1210,6 +1211,7 @@ typedef struct _SHCore {

// Language parsing and evaluation
SHReadProc read;
SHLoadAstProc loadAst;
SHCreateEvalEnv createEvalEnv;
SHFreeEvalEnv freeEvalEnv;
SHEvalProc eval;
Expand Down
35 changes: 35 additions & 0 deletions include/shards/shards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,41 @@ class Shards {
let wireController = WireController(native: wire.wire.pointee!)
return wireController
}

static func evalWire(_ name: String, _ ast: [UInt8]) -> WireController? {
// Create SHStringWithLen instances
let nameStr = SwiftSWL(name)

// Read the AST
var ast = ast.withUnsafeBufferPointer { buffer in
G.Core.pointee.loadAst(buffer.baseAddress!, UInt32(buffer.count))
}
guard ast.error == nil else {
return nil
}

// Create evaluation environment
let emptyStr = SHStringWithLen.fromStatic("")
let env = G.Core.pointee.createEvalEnv(emptyStr)

// Evaluate the AST
let error = G.Core.pointee.eval(env, &ast.ast) // consumes ast
guard error == nil else {
G.Core.pointee.freeEvalEnv(env)
return nil
}

// Transform environment into a wire
var wire = G.Core.pointee.transformEnv(env, nameStr.asSHStringWithLen()) // consumes env
guard wire.error == nil else {
G.Core.pointee.freeWire(&wire)
return nil
}

// Create WireController from the resulting wire
let wireController = WireController(native: wire.wire.pointee!)
return wireController
}
}

#if canImport(UIKit)
Expand Down
3 changes: 3 additions & 0 deletions shards/core/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2570,6 +2570,7 @@ SHCore sh_current_interface{};
extern "C" {
SHLAst shards_read(SHStringWithLen name, SHStringWithLen code, SHStringWithLen base_path, const SHStringWithLen *include_dirs,
uint32_t num_include_dirs);
SHLAst shards_load_ast(const uint8_t *bytes, uint32_t size);
SHLEvalEnv *shards_create_env(SHStringWithLen namespace_);
void shards_free_env(SHLEvalEnv *env);
SHLError *shards_eval_env(SHLEvalEnv *env, const SHVar *ast);
Expand Down Expand Up @@ -3095,6 +3096,8 @@ SHCore *__cdecl shardsInterface(uint32_t abi_version) {
result->read = [](SHStringWithLen name, SHStringWithLen code, SHStringWithLen basePath, const SHStringWithLen *includeDirs,
uint32_t numIncludeDirs) { return shards_read(name, code, basePath, includeDirs, numIncludeDirs); };

result->loadAst = [](const uint8_t *bytes, uint32_t size) { return shards_load_ast(bytes, size); };

result->createEvalEnv = [](SHStringWithLen namespace_) { return shards_create_env(namespace_); };

result->freeEvalEnv = [](SHLEvalEnv *env) { shards_free_env(env); };
Expand Down
2 changes: 1 addition & 1 deletion shards/langffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub extern "C" fn shards_read(
}

#[no_mangle]
pub extern "C" fn shards_load_ast(bytes: *mut u8, size: u32) -> SHLAst {
pub extern "C" fn shards_load_ast(bytes: *const u8, size: u32) -> SHLAst {
let bytes = unsafe { from_raw_parts_allow_null(bytes, size as usize) };
let decoded_bin: Result<Program, _> = flexbuffers::from_slice(bytes);
match decoded_bin {
Expand Down

0 comments on commit d21e916

Please sign in to comment.