Skip to content

Commit b3c98e9

Browse files
committed
Remove context pointer
1 parent 9752eb1 commit b3c98e9

File tree

13 files changed

+453
-707
lines changed

13 files changed

+453
-707
lines changed

api/examples/echo.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ impl Deserialize for Value {
5555
// special case to exercise string interning and get_obj_prop
5656
let raw_value = match key.as_str() {
5757
"foo" => {
58-
let interned_string_id = FOO_INTERNED_STRING_ID.load_from_value(value);
58+
let interned_string_id = FOO_INTERNED_STRING_ID.load();
5959
value.get_interned_obj_prop(interned_string_id)
6060
}
6161
"bar" => {
62-
let interned_string_id = BAR_INTERNED_STRING_ID.load_from_value(value);
62+
let interned_string_id = BAR_INTERNED_STRING_ID.load();
6363
value.get_interned_obj_prop(interned_string_id)
6464
}
6565
"abc" | "def" => value.get_obj_prop(key.as_str()),
@@ -94,13 +94,11 @@ impl Serialize for Value {
9494
for (key, value) in object {
9595
match key.as_str() {
9696
"foo" => {
97-
let interned_string_id =
98-
FOO_INTERNED_STRING_ID.load_from_context(ctx);
97+
let interned_string_id = FOO_INTERNED_STRING_ID.load();
9998
ctx.write_interned_utf8_str(interned_string_id)?;
10099
}
101100
"bar" => {
102-
let interned_string_id =
103-
BAR_INTERNED_STRING_ID.load_from_context(ctx);
101+
let interned_string_id = BAR_INTERNED_STRING_ID.load();
104102
ctx.write_interned_utf8_str(interned_string_id)?;
105103
}
106104
_ => ctx.write_utf8_str(key)?,
@@ -138,20 +136,4 @@ mod tests {
138136

139137
assert_eq!(result, Value::Object(Vec::new()));
140138
}
141-
142-
#[test]
143-
fn test_echo_multiple_contexts_with_interned_string_cache() {
144-
// tests the cached interned string logic by having multiple contexts that
145-
// hit the interned string cache
146-
let input = serde_json::json!({ "foo": "bar"});
147-
let context = Context::new_with_input(input.clone());
148-
let api_value = context.input_get().unwrap();
149-
let input_value: Value = Deserialize::deserialize(&api_value).unwrap();
150-
let result = echo(input_value);
151-
let context2 = Context::new_with_input(input);
152-
let api_value2 = context2.input_get().unwrap();
153-
let input_value2: Value = Deserialize::deserialize(&api_value2).unwrap();
154-
let result2 = echo(input_value2);
155-
assert_eq!(result, result2);
156-
}
157139
}

api/src/lib.rs

Lines changed: 110 additions & 229 deletions
Large diffs are not rendered by default.

api/src/shopify_function.h

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <stddef.h>
66

77
// Type definitions
8-
typedef int32_t ContextPtr;
98
typedef int64_t Val;
109
typedef int32_t WriteResult;
1110
typedef size_t InternedStringId;
@@ -20,206 +19,186 @@ typedef size_t InternedStringId;
2019
// Common API
2120
/**
2221
* Creates a new context for the Shopify Function execution
23-
* @return A new context pointer
2422
*/
2523
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
2624
__attribute__((import_name("shopify_function_context_new")))
27-
extern ContextPtr shopify_function_context_new(void);
25+
extern void shopify_function_context_new(void);
2826

2927
// Read API
3028
/**
3129
* Gets the input value from the context
32-
* @param context The context pointer
3330
* @return The input value
3431
*/
3532
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
3633
__attribute__((import_name("shopify_function_input_get")))
37-
extern Val shopify_function_input_get(ContextPtr context);
34+
extern Val shopify_function_input_get();
3835

3936
/**
4037
* Gets the length of a value (for arrays, objects, or strings)
41-
* @param context The context pointer
4238
* @param scope The value to get the length of
4339
* @return The length of the value
4440
*/
4541
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
4642
__attribute__((import_name("shopify_function_input_get_val_len")))
47-
extern size_t shopify_function_input_get_val_len(ContextPtr context, Val scope);
43+
extern size_t shopify_function_input_get_val_len(Val scope);
4844

4945
/**
5046
* Reads a UTF-8 encoded string from the input into the provided buffer
51-
* @param context The context pointer
5247
* @param src The source address of the string
5348
* @param out The output buffer to write the string to
5449
* @param len The length of the string
5550
*/
5651
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
5752
__attribute__((import_name("shopify_function_input_read_utf8_str")))
58-
extern void shopify_function_input_read_utf8_str(ContextPtr context, size_t src, uint8_t* out, size_t len);
53+
extern void shopify_function_input_read_utf8_str(size_t src, uint8_t* out, size_t len);
5954

6055
/**
6156
* Gets an object property by name
62-
* @param context The context pointer
6357
* @param scope The object to get the property from
6458
* @param ptr The property name (as a UTF-8 string)
6559
* @param len The length of the property name
6660
* @return The property value
6761
*/
6862
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
6963
__attribute__((import_name("shopify_function_input_get_obj_prop")))
70-
extern Val shopify_function_input_get_obj_prop(ContextPtr context, Val scope, const uint8_t* ptr, size_t len);
64+
extern Val shopify_function_input_get_obj_prop(Val scope, const uint8_t* ptr, size_t len);
7165

7266
/**
7367
* Gets an object property by interned string ID
74-
* @param context The context pointer
7568
* @param scope The object to get the property from
7669
* @param interned_string_id The interned string ID of the property name
7770
* @return The property value
7871
*/
7972
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
8073
__attribute__((import_name("shopify_function_input_get_interned_obj_prop")))
81-
extern Val shopify_function_input_get_interned_obj_prop(ContextPtr context, Val scope, InternedStringId interned_string_id);
74+
extern Val shopify_function_input_get_interned_obj_prop(Val scope, InternedStringId interned_string_id);
8275

8376
/**
8477
* Gets an element from an array by index
85-
* @param context The context pointer
8678
* @param scope The array to get the element from
8779
* @param index The index of the element
8880
* @return The element value
8981
*/
9082
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
9183
__attribute__((import_name("shopify_function_input_get_at_index")))
92-
extern Val shopify_function_input_get_at_index(ContextPtr context, Val scope, size_t index);
84+
extern Val shopify_function_input_get_at_index(Val scope, size_t index);
9385

9486
/**
9587
* Gets an object key at the specified index
96-
* @param context The context pointer
9788
* @param scope The object to get the key from
9889
* @param index The index of the key
9990
* @return The key value (as a string)
10091
*/
10192
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
10293
__attribute__((import_name("shopify_function_input_get_obj_key_at_index")))
103-
extern Val shopify_function_input_get_obj_key_at_index(ContextPtr context, Val scope, size_t index);
94+
extern Val shopify_function_input_get_obj_key_at_index(Val scope, size_t index);
10495

10596
// Write API
10697
/**
10798
* Creates a new boolean output value
108-
* @param context The context pointer
10999
* @param value The boolean value (0 for false, non-zero for true)
110100
* @return WriteResult indicating success or failure
111101
*/
112102
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
113103
__attribute__((import_name("shopify_function_output_new_bool")))
114-
extern WriteResult shopify_function_output_new_bool(ContextPtr context, uint32_t value);
104+
extern WriteResult shopify_function_output_new_bool(uint32_t value);
115105

116106
/**
117107
* Creates a new null output value
118-
* @param context The context pointer
119108
* @return WriteResult indicating success or failure
120109
*/
121110
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
122111
__attribute__((import_name("shopify_function_output_new_null")))
123-
extern WriteResult shopify_function_output_new_null(ContextPtr context);
112+
extern WriteResult shopify_function_output_new_null();
124113

125114
/**
126115
* Finalizes the output and returns the result
127-
* @param context The context pointer
128116
* @return WriteResult indicating success or failure
129117
*/
130118
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
131119
__attribute__((import_name("shopify_function_output_finalize")))
132-
extern WriteResult shopify_function_output_finalize(ContextPtr context);
120+
extern WriteResult shopify_function_output_finalize();
133121

134122
/**
135123
* Creates a new 32-bit integer output value
136-
* @param context The context pointer
137124
* @param value The integer value
138125
* @return WriteResult indicating success or failure
139126
*/
140127
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
141128
__attribute__((import_name("shopify_function_output_new_i32")))
142-
extern WriteResult shopify_function_output_new_i32(ContextPtr context, int32_t value);
129+
extern WriteResult shopify_function_output_new_i32(int32_t value);
143130

144131
/**
145132
* Creates a new 64-bit float output value
146-
* @param context The context pointer
147133
* @param value The float value
148134
* @return WriteResult indicating success or failure
149135
*/
150136
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
151137
__attribute__((import_name("shopify_function_output_new_f64")))
152-
extern WriteResult shopify_function_output_new_f64(ContextPtr context, double value);
138+
extern WriteResult shopify_function_output_new_f64(double value);
153139

154140
/**
155141
* Creates a new UTF-8 string output value
156-
* @param context The context pointer
157142
* @param ptr The string data
158143
* @param len The length of the string
159144
* @return WriteResult indicating success or failure
160145
*/
161146
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
162147
__attribute__((import_name("shopify_function_output_new_utf8_str")))
163-
extern WriteResult shopify_function_output_new_utf8_str(ContextPtr context, const uint8_t* ptr, size_t len);
148+
extern WriteResult shopify_function_output_new_utf8_str(const uint8_t* ptr, size_t len);
164149

165150
/**
166151
* Creates a new UTF-8 string output value from an interned string ID
167-
* @param context The context pointer
168152
* @param id The interned string ID
169153
* @return WriteResult indicating success or failure
170154
*/
171155
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
172156
__attribute__((import_name("shopify_function_output_new_interned_utf8_str")))
173-
extern WriteResult shopify_function_output_new_interned_utf8_str(ContextPtr context, InternedStringId id);
157+
extern WriteResult shopify_function_output_new_interned_utf8_str(InternedStringId id);
174158

175159
/**
176160
* Creates a new object output value with the specified number of properties
177-
* @param context The context pointer
178161
* @param len The number of properties
179162
* @return WriteResult indicating success or failure
180163
*/
181164
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
182165
__attribute__((import_name("shopify_function_output_new_object")))
183-
extern WriteResult shopify_function_output_new_object(ContextPtr context, size_t len);
166+
extern WriteResult shopify_function_output_new_object(size_t len);
184167

185168
/**
186169
* Finalizes an object output value
187-
* @param context The context pointer
188170
* @return WriteResult indicating success or failure
189171
*/
190172
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
191173
__attribute__((import_name("shopify_function_output_finish_object")))
192-
extern WriteResult shopify_function_output_finish_object(ContextPtr context);
174+
extern WriteResult shopify_function_output_finish_object();
193175

194176
/**
195177
* Creates a new array output value with the specified length
196-
* @param context The context pointer
197178
* @param len The length of the array
198179
* @return WriteResult indicating success or failure
199180
*/
200181
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
201182
__attribute__((import_name("shopify_function_output_new_array")))
202-
extern WriteResult shopify_function_output_new_array(ContextPtr context, size_t len);
183+
extern WriteResult shopify_function_output_new_array(size_t len);
203184

204185
/**
205186
* Finalizes an array output value
206-
* @param context The context pointer
207187
* @return WriteResult indicating success or failure
208188
*/
209189
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
210190
__attribute__((import_name("shopify_function_output_finish_array")))
211-
extern WriteResult shopify_function_output_finish_array(ContextPtr context);
191+
extern WriteResult shopify_function_output_finish_array();
212192

213193
// Other
214194
/**
215195
* Interns a UTF-8 string and returns its ID for efficient reuse
216-
* @param context The context pointer
217196
* @param ptr The string data
218197
* @param len The length of the string
219198
* @return The interned string ID
220199
*/
221200
__attribute__((import_module(SHOPIFY_FUNCTION_IMPORT_MODULE)))
222201
__attribute__((import_name("shopify_function_intern_utf8_str")))
223-
extern InternedStringId shopify_function_intern_utf8_str(ContextPtr context, const uint8_t* ptr, size_t len);
202+
extern InternedStringId shopify_function_intern_utf8_str(const uint8_t* ptr, size_t len);
224203

225204
#endif // SHOPIFY_FUNCTION_H

0 commit comments

Comments
 (0)