diff options
| author | 2022-05-16 15:46:20 -0700 | |
|---|---|---|
| committer | 2022-05-16 15:46:20 -0700 | |
| commit | a37f86e89dc01f884a1b4474c27c79d5932093a0 (patch) | |
| tree | 4732a1a1c032f2e6788f3b8d7151c5d5db15fb38 /src/global.zig | |
| parent | 2bd0dcfdfaf6c385e927570b0e102385dc8c3975 (diff) | |
| download | bun-a37f86e89dc01f884a1b4474c27c79d5932093a0.tar.gz bun-a37f86e89dc01f884a1b4474c27c79d5932093a0.tar.zst bun-a37f86e89dc01f884a1b4474c27c79d5932093a0.zip | |
`bun:sqlite` (#167)
* :scissors:
* Add the slow version
* draw the rest of the owl
* Fix crash when allocating lots of memory
* [Bun.Transipiler] Support passing objects
* [JS Parser] Support passing objects to macros via Bun.Transpiler
* Update JSSQLStatement.cpp
* Embed SQLite
* Add SQLite to Dockerfile
* [sqlite] Add quick one-off queries without creating a whole object
* [sqlite] Add `columnsCount`, rename raw() to `values()`, remove `rebind`
* Implement `bun:sqlite`
* return null
* Fix updating query
* Update bun.d.ts
* more tests
* Support variadic arguments, write tests and add types
* Update sqlite.d.ts
* Update sqlite.d.ts
* latest
* Implement `Database.loadExtension` and `Database.setCustomSQLite`
* Support `require.resolve`
* [napi] Improve string performance
* [bun.js] Support some of `node:module`
* another test
* [sqlite] Support serialize & deserialize
* [`bun:ffi`] Implement `CFunction` and `linkSymbols`
* [bun.js] Fix crash in `Buffer.from`
* Update sqlite.test.js
* Document linkSymbols
* docs
* Update README.md
Diffstat (limited to 'src/global.zig')
| -rw-r--r-- | src/global.zig | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/global.zig b/src/global.zig index 25b908543..e90a1beec 100644 --- a/src/global.zig +++ b/src/global.zig @@ -95,3 +95,64 @@ pub const MAX_PATH_BYTES: usize = if (Environment.isWasm) 1024 else std.fs.MAX_P pub inline fn cast(comptime To: type, value: anytype) To { return @ptrCast(To, @alignCast(@alignOf(To), value)); } + +extern fn strlen(ptr: [*c]const u8) usize; +pub fn indexOfSentinel(comptime Elem: type, comptime sentinel: Elem, ptr: [*:sentinel]const Elem) usize { + if (comptime Elem == u8 and sentinel == 0) { + return strlen(ptr); + } else { + var i: usize = 0; + while (ptr[i] != sentinel) { + i += 1; + } + return i; + } +} + +pub fn len(value: anytype) usize { + return switch (@typeInfo(@TypeOf(value))) { + .Array => |info| info.len, + .Vector => |info| info.len, + .Pointer => |info| switch (info.size) { + .One => switch (@typeInfo(info.child)) { + .Array => value.len, + else => @compileError("invalid type given to std.mem.len"), + }, + .Many => { + const sentinel_ptr = info.sentinel orelse + @compileError("length of pointer with no sentinel"); + const sentinel = @ptrCast(*const info.child, sentinel_ptr).*; + + return indexOfSentinel(info.child, sentinel, value); + }, + .C => { + std.debug.assert(value != null); + return indexOfSentinel(info.child, 0, value); + }, + .Slice => value.len, + }, + .Struct => |info| if (info.is_tuple) { + return info.fields.len; + } else @compileError("invalid type given to std.mem.len"), + else => @compileError("invalid type given to std.mem.len"), + }; +} + +pub fn span(ptr: anytype) std.mem.Span(@TypeOf(ptr)) { + if (@typeInfo(@TypeOf(ptr)) == .Optional) { + if (ptr) |non_null| { + return span(non_null); + } else { + return null; + } + } + const Result = std.mem.Span(@TypeOf(ptr)); + const l = len(ptr); + const ptr_info = @typeInfo(Result).Pointer; + if (ptr_info.sentinel) |s_ptr| { + const s = @ptrCast(*const ptr_info.child, s_ptr).*; + return ptr[0..l :s]; + } else { + return ptr[0..l]; + } +} |
