Skip to content

Commit 050fda4

Browse files
committed
moving primitives out of marcos and adding type boundaries
1 parent 45513f2 commit 050fda4

File tree

2 files changed

+209
-14
lines changed

2 files changed

+209
-14
lines changed

src/gen/oracle.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,17 @@ impl DartCodeOracle {
215215

216216
pub fn type_lower_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
217217
match ty {
218-
Type::UInt32
219-
| Type::Int8
220-
| Type::UInt8
218+
// Float types don't need validation, just pass through
219+
Type::Float32 | Type::Float64 => inner,
220+
// All integer types now have validation and need lowering
221+
Type::Int8
221222
| Type::Int16
222-
| Type::UInt16
223223
| Type::Int32
224-
| Type::Float32
225-
| Type::Float64 => inner,
226-
// 64-bit integers need lowering because they use BigInt
227-
Type::Int64 | Type::UInt64 => {
224+
| Type::Int64
225+
| Type::UInt8
226+
| Type::UInt16
227+
| Type::UInt32
228+
| Type::UInt64 => {
228229
quote!($(ty.as_codetype().ffi_converter_name()).lower($inner))
229230
}
230231
Type::Boolean

src/gen/primitives/mod.rs

Lines changed: 200 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ impl_code_type_for_primitive!(Float32CodeType, "double", "Double32");
6969
impl_code_type_for_primitive!(Float64CodeType, "double", "Double64");
7070

7171
impl_renderable_for_primitive!(BytesCodeType, "Uint8List", "Uint8List");
72-
impl_renderable_for_primitive!(Int8CodeType, "int", "Int8", 1);
73-
impl_renderable_for_primitive!(Int16CodeType, "int", "Int16", 2);
74-
impl_renderable_for_primitive!(Int32CodeType, "int", "Int32", 4);
75-
impl_renderable_for_primitive!(UInt8CodeType, "int", "UInt8", 1);
76-
impl_renderable_for_primitive!(UInt16CodeType, "int", "UInt16", 2);
77-
impl_renderable_for_primitive!(UInt32CodeType, "int", "UInt32", 4);
72+
// Custom implementations for integer types with bounds checking
7873
impl_renderable_for_primitive!(Float32CodeType, "double", "Double32", 4);
7974
impl_renderable_for_primitive!(Float64CodeType, "double", "Double64", 8);
8075

@@ -161,3 +156,202 @@ impl Renderable for UInt64CodeType {
161156
}
162157
}
163158
}
159+
160+
// Custom implementations for smaller integer types with bounds checking
161+
impl Renderable for Int8CodeType {
162+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
163+
let cl_name = &self.ffi_converter_name();
164+
let type_signature = &self.type_label();
165+
166+
quote! {
167+
class $cl_name {
168+
static $type_signature lift($type_signature value) => value;
169+
170+
static LiftRetVal<$type_signature> read(Uint8List buf) {
171+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getInt8(0), 1);
172+
}
173+
174+
static $type_signature lower($type_signature value) {
175+
if (value < -128 || value > 127) {
176+
throw ArgumentError("Value out of range for i8: " + value.toString());
177+
}
178+
return value;
179+
}
180+
181+
static int allocationSize([$type_signature value = 0]) {
182+
return 1;
183+
}
184+
185+
static int write($type_signature value, Uint8List buf) {
186+
buf.buffer.asByteData(buf.offsetInBytes).setInt8(0, lower(value));
187+
return 1;
188+
}
189+
}
190+
}
191+
}
192+
}
193+
194+
impl Renderable for Int16CodeType {
195+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
196+
let cl_name = &self.ffi_converter_name();
197+
let type_signature = &self.type_label();
198+
199+
quote! {
200+
class $cl_name {
201+
static $type_signature lift($type_signature value) => value;
202+
203+
static LiftRetVal<$type_signature> read(Uint8List buf) {
204+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getInt16(0), 2);
205+
}
206+
207+
static $type_signature lower($type_signature value) {
208+
if (value < -32768 || value > 32767) {
209+
throw ArgumentError("Value out of range for i16: " + value.toString());
210+
}
211+
return value;
212+
}
213+
214+
static int allocationSize([$type_signature value = 0]) {
215+
return 2;
216+
}
217+
218+
static int write($type_signature value, Uint8List buf) {
219+
buf.buffer.asByteData(buf.offsetInBytes).setInt16(0, lower(value));
220+
return 2;
221+
}
222+
}
223+
}
224+
}
225+
}
226+
227+
impl Renderable for Int32CodeType {
228+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
229+
let cl_name = &self.ffi_converter_name();
230+
let type_signature = &self.type_label();
231+
232+
quote! {
233+
class $cl_name {
234+
static $type_signature lift($type_signature value) => value;
235+
236+
static LiftRetVal<$type_signature> read(Uint8List buf) {
237+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getInt32(0), 4);
238+
}
239+
240+
static $type_signature lower($type_signature value) {
241+
if (value < -2147483648 || value > 2147483647) {
242+
throw ArgumentError("Value out of range for i32: " + value.toString());
243+
}
244+
return value;
245+
}
246+
247+
static int allocationSize([$type_signature value = 0]) {
248+
return 4;
249+
}
250+
251+
static int write($type_signature value, Uint8List buf) {
252+
buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, lower(value));
253+
return 4;
254+
}
255+
}
256+
}
257+
}
258+
}
259+
260+
impl Renderable for UInt8CodeType {
261+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
262+
let cl_name = &self.ffi_converter_name();
263+
let type_signature = &self.type_label();
264+
265+
quote! {
266+
class $cl_name {
267+
static $type_signature lift($type_signature value) => value;
268+
269+
static LiftRetVal<$type_signature> read(Uint8List buf) {
270+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getUint8(0), 1);
271+
}
272+
273+
static $type_signature lower($type_signature value) {
274+
if (value < 0 || value > 255) {
275+
throw ArgumentError("Value out of range for u8: " + value.toString());
276+
}
277+
return value;
278+
}
279+
280+
static int allocationSize([$type_signature value = 0]) {
281+
return 1;
282+
}
283+
284+
static int write($type_signature value, Uint8List buf) {
285+
buf.buffer.asByteData(buf.offsetInBytes).setUint8(0, lower(value));
286+
return 1;
287+
}
288+
}
289+
}
290+
}
291+
}
292+
293+
impl Renderable for UInt16CodeType {
294+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
295+
let cl_name = &self.ffi_converter_name();
296+
let type_signature = &self.type_label();
297+
298+
quote! {
299+
class $cl_name {
300+
static $type_signature lift($type_signature value) => value;
301+
302+
static LiftRetVal<$type_signature> read(Uint8List buf) {
303+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getUint16(0), 2);
304+
}
305+
306+
static $type_signature lower($type_signature value) {
307+
if (value < 0 || value > 65535) {
308+
throw ArgumentError("Value out of range for u16: " + value.toString());
309+
}
310+
return value;
311+
}
312+
313+
static int allocationSize([$type_signature value = 0]) {
314+
return 2;
315+
}
316+
317+
static int write($type_signature value, Uint8List buf) {
318+
buf.buffer.asByteData(buf.offsetInBytes).setUint16(0, lower(value));
319+
return 2;
320+
}
321+
}
322+
}
323+
}
324+
}
325+
326+
impl Renderable for UInt32CodeType {
327+
fn render_type_helper(&self, _type_helper: &dyn TypeHelperRenderer) -> dart::Tokens {
328+
let cl_name = &self.ffi_converter_name();
329+
let type_signature = &self.type_label();
330+
331+
quote! {
332+
class $cl_name {
333+
static $type_signature lift($type_signature value) => value;
334+
335+
static LiftRetVal<$type_signature> read(Uint8List buf) {
336+
return LiftRetVal(buf.buffer.asByteData(buf.offsetInBytes).getUint32(0), 4);
337+
}
338+
339+
static $type_signature lower($type_signature value) {
340+
if (value < 0 || value > 4294967295) {
341+
throw ArgumentError("Value out of range for u32: " + value.toString());
342+
}
343+
return value;
344+
}
345+
346+
static int allocationSize([$type_signature value = 0]) {
347+
return 4;
348+
}
349+
350+
static int write($type_signature value, Uint8List buf) {
351+
buf.buffer.asByteData(buf.offsetInBytes).setUint32(0, lower(value));
352+
return 4;
353+
}
354+
}
355+
}
356+
}
357+
}

0 commit comments

Comments
 (0)