Skip to content

Remove context pointer #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 4 additions & 22 deletions api/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ impl Deserialize for Value {
// special case to exercise string interning and get_obj_prop
let raw_value = match key.as_str() {
"foo" => {
let interned_string_id = FOO_INTERNED_STRING_ID.load_from_value(value);
let interned_string_id = FOO_INTERNED_STRING_ID.load();
value.get_interned_obj_prop(interned_string_id)
}
"bar" => {
let interned_string_id = BAR_INTERNED_STRING_ID.load_from_value(value);
let interned_string_id = BAR_INTERNED_STRING_ID.load();
value.get_interned_obj_prop(interned_string_id)
}
"abc" | "def" => value.get_obj_prop(key.as_str()),
Expand Down Expand Up @@ -94,13 +94,11 @@ impl Serialize for Value {
for (key, value) in object {
match key.as_str() {
"foo" => {
let interned_string_id =
FOO_INTERNED_STRING_ID.load_from_context(ctx);
let interned_string_id = FOO_INTERNED_STRING_ID.load();
ctx.write_interned_utf8_str(interned_string_id)?;
}
"bar" => {
let interned_string_id =
BAR_INTERNED_STRING_ID.load_from_context(ctx);
let interned_string_id = BAR_INTERNED_STRING_ID.load();
ctx.write_interned_utf8_str(interned_string_id)?;
}
_ => ctx.write_utf8_str(key)?,
Expand Down Expand Up @@ -138,20 +136,4 @@ mod tests {

assert_eq!(result, Value::Object(Vec::new()));
}

#[test]
fn test_echo_multiple_contexts_with_interned_string_cache() {
// tests the cached interned string logic by having multiple contexts that
// hit the interned string cache
let input = serde_json::json!({ "foo": "bar"});
let context = Context::new_with_input(input.clone());
let api_value = context.input_get().unwrap();
let input_value: Value = Deserialize::deserialize(&api_value).unwrap();
let result = echo(input_value);
let context2 = Context::new_with_input(input);
let api_value2 = context2.input_get().unwrap();
let input_value2: Value = Deserialize::deserialize(&api_value2).unwrap();
let result2 = echo(input_value2);
assert_eq!(result, result2);
}
}
334 changes: 94 additions & 240 deletions api/src/lib.rs

Large diffs are not rendered by default.

61 changes: 20 additions & 41 deletions api/src/shopify_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <stddef.h>

// Type definitions
typedef int32_t ContextPtr;
typedef int64_t Val;
typedef int32_t WriteResult;
typedef size_t InternedStringId;
Expand All @@ -20,206 +19,186 @@ typedef size_t InternedStringId;
// Common API
/**
* Creates a new context for the Shopify Function execution
* @return A new context pointer
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_context_new")))
extern ContextPtr shopify_function_context_new(void);
extern void shopify_function_context_new(void);

// Read API
/**
* Gets the input value from the context
* @param context The context pointer
* @return The input value
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get")))
extern Val shopify_function_input_get(ContextPtr context);
extern Val shopify_function_input_get();

/**
* Gets the length of a value (for arrays, objects, or strings)
* @param context The context pointer
* @param scope The value to get the length of
* @return The length of the value
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get_val_len")))
extern size_t shopify_function_input_get_val_len(ContextPtr context, Val scope);
extern size_t shopify_function_input_get_val_len(Val scope);

/**
* Reads a UTF-8 encoded string from the input into the provided buffer
* @param context The context pointer
* @param src The source address of the string
* @param out The output buffer to write the string to
* @param len The length of the string
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_read_utf8_str")))
extern void shopify_function_input_read_utf8_str(ContextPtr context, size_t src, uint8_t* out, size_t len);
extern void shopify_function_input_read_utf8_str(size_t src, uint8_t* out, size_t len);

/**
* Gets an object property by name
* @param context The context pointer
* @param scope The object to get the property from
* @param ptr The property name (as a UTF-8 string)
* @param len The length of the property name
* @return The property value
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get_obj_prop")))
extern Val shopify_function_input_get_obj_prop(ContextPtr context, Val scope, const uint8_t* ptr, size_t len);
extern Val shopify_function_input_get_obj_prop(Val scope, const uint8_t* ptr, size_t len);

/**
* Gets an object property by interned string ID
* @param context The context pointer
* @param scope The object to get the property from
* @param interned_string_id The interned string ID of the property name
* @return The property value
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get_interned_obj_prop")))
extern Val shopify_function_input_get_interned_obj_prop(ContextPtr context, Val scope, InternedStringId interned_string_id);
extern Val shopify_function_input_get_interned_obj_prop(Val scope, InternedStringId interned_string_id);

/**
* Gets an element from an array by index
* @param context The context pointer
* @param scope The array to get the element from
* @param index The index of the element
* @return The element value
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get_at_index")))
extern Val shopify_function_input_get_at_index(ContextPtr context, Val scope, size_t index);
extern Val shopify_function_input_get_at_index(Val scope, size_t index);

/**
* Gets an object key at the specified index
* @param context The context pointer
* @param scope The object to get the key from
* @param index The index of the key
* @return The key value (as a string)
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_input_get_obj_key_at_index")))
extern Val shopify_function_input_get_obj_key_at_index(ContextPtr context, Val scope, size_t index);
extern Val shopify_function_input_get_obj_key_at_index(Val scope, size_t index);

// Write API
/**
* Creates a new boolean output value
* @param context The context pointer
* @param value The boolean value (0 for false, non-zero for true)
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_bool")))
extern WriteResult shopify_function_output_new_bool(ContextPtr context, uint32_t value);
extern WriteResult shopify_function_output_new_bool(uint32_t value);

/**
* Creates a new null output value
* @param context The context pointer
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_null")))
extern WriteResult shopify_function_output_new_null(ContextPtr context);
extern WriteResult shopify_function_output_new_null();

/**
* Finalizes the output and returns the result
* @param context The context pointer
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_finalize")))
extern WriteResult shopify_function_output_finalize(ContextPtr context);
extern WriteResult shopify_function_output_finalize();

/**
* Creates a new 32-bit integer output value
* @param context The context pointer
* @param value The integer value
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_i32")))
extern WriteResult shopify_function_output_new_i32(ContextPtr context, int32_t value);
extern WriteResult shopify_function_output_new_i32(int32_t value);

/**
* Creates a new 64-bit float output value
* @param context The context pointer
* @param value The float value
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_f64")))
extern WriteResult shopify_function_output_new_f64(ContextPtr context, double value);
extern WriteResult shopify_function_output_new_f64(double value);

/**
* Creates a new UTF-8 string output value
* @param context The context pointer
* @param ptr The string data
* @param len The length of the string
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_utf8_str")))
extern WriteResult shopify_function_output_new_utf8_str(ContextPtr context, const uint8_t* ptr, size_t len);
extern WriteResult shopify_function_output_new_utf8_str(const uint8_t* ptr, size_t len);

/**
* Creates a new UTF-8 string output value from an interned string ID
* @param context The context pointer
* @param id The interned string ID
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_interned_utf8_str")))
extern WriteResult shopify_function_output_new_interned_utf8_str(ContextPtr context, InternedStringId id);
extern WriteResult shopify_function_output_new_interned_utf8_str(InternedStringId id);

/**
* Creates a new object output value with the specified number of properties
* @param context The context pointer
* @param len The number of properties
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_object")))
extern WriteResult shopify_function_output_new_object(ContextPtr context, size_t len);
extern WriteResult shopify_function_output_new_object(size_t len);

/**
* Finalizes an object output value
* @param context The context pointer
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_finish_object")))
extern WriteResult shopify_function_output_finish_object(ContextPtr context);
extern WriteResult shopify_function_output_finish_object();

/**
* Creates a new array output value with the specified length
* @param context The context pointer
* @param len The length of the array
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_new_array")))
extern WriteResult shopify_function_output_new_array(ContextPtr context, size_t len);
extern WriteResult shopify_function_output_new_array(size_t len);

/**
* Finalizes an array output value
* @param context The context pointer
* @return WriteResult indicating success or failure
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_output_finish_array")))
extern WriteResult shopify_function_output_finish_array(ContextPtr context);
extern WriteResult shopify_function_output_finish_array();

// Other
/**
* Interns a UTF-8 string and returns its ID for efficient reuse
* @param context The context pointer
* @param ptr The string data
* @param len The length of the string
* @return The interned string ID
*/
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
__attribute__((import_name("shopify_function_intern_utf8_str")))
extern InternedStringId shopify_function_intern_utf8_str(ContextPtr context, const uint8_t* ptr, size_t len);
extern InternedStringId shopify_function_intern_utf8_str(const uint8_t* ptr, size_t len);

#endif // SHOPIFY_FUNCTION_H
Loading