5.2.8. C Integration: Serialization
This tutorial demonstrates how to serialize a compiled daslang program to a binary blob and deserialize it back, skipping recompilation on subsequent runs.
The workflow:
Compile a program normally with
das_program_compileSimulate it at least once (resolves function pointers)
Serialize to an in-memory buffer
Release the original program
Deserialize from the buffer — no parsing, no type inference
Simulate and run as usual
This is useful for faster startup, distributing pre-compiled scripts, and caching compiled programs in editors or build pipelines.
5.2.8.1. Serialization API
Three functions make up the serialization API:
Function |
Purpose |
|---|---|
|
Serialize to binary; returns opaque handle |
|
Deserialize from raw bytes; returns program |
|
Free the serialization buffer |
5.2.8.2. Serializing
const void * blob_data = NULL;
int64_t blob_size = 0;
das_serialized_data * blob = das_program_serialize(program, &blob_data, &blob_size);
printf("Serialized size: %lld bytes\n", (long long)blob_size);
// Save blob_data/blob_size to file for persistent caching if desired.
The program must have been simulated at least once before serialization.
5.2.8.3. Deserializing
das_program * restored = das_program_deserialize(blob_data, blob_size);
// The blob can be released after deserialization — the program is independent.
das_serialized_data_release(blob);
// Simulate and run as usual
das_context * ctx = das_context_make(das_program_context_stack_size(restored));
das_program_simulate(restored, ctx, tout);
das_function * fn = das_context_find_function(ctx, "test");
das_context_eval_with_catch(ctx, fn, NULL);
5.2.8.4. Build & run
Build:
cmake --build build --config Release --target integration_c_08
Run:
bin/Release/integration_c_08
Expected output:
=== Stage 1: Compile from source ===
Compiled successfully.
Simulated successfully.
=== Stage 2: Serialize ===
Serialized size: 5022 bytes
Original program released.
=== Stage 3: Deserialize ===
Deserialized successfully.
=== Stage 4: Simulate and run ===
=== Serialization Tutorial ===
Hello, World!
sum_to(10) = 55
sum_to(100) = 5050
See also
Full source:
08_serialization.c
This tutorial reuses the script from the C++ serialization tutorial:
14_serialization.das
Previous tutorial: tutorial_integration_c_context_variables
Next tutorial: tutorial_integration_c_aot
C++ equivalent: tutorial_integration_cpp_serialization
daScriptC.h API header: include/daScript/daScriptC.h