damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 30e33a01c17649c524496de1179301d37b18e557
parent a6cbf50def9ad316aae058bfb4b300306d3cb447
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  3 Jul 2023 16:59:50 -0700

nostrscript: add a helper function

Diffstat:
Mdamus-c/nostrscript.c | 18++++++++++++++++++
Mdamus-c/nostrscript.h | 1+
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/Models/UserSettingsStore.swift | 2+-
MdamusTests/NostrScriptTests.swift | 36++++++++++++++++++++++++++++++++++--
Mnostrscript/NostrScript.swift | 17+++++++++++++++++
Anostrscript/bool_setting.ts | 18++++++++++++++++++
Anostrscript/bool_setting.wasm | 0
Mnostrscript/nostr.ts | 5+++++
9 files changed, 98 insertions(+), 3 deletions(-)

diff --git a/damus-c/nostrscript.c b/damus-c/nostrscript.c @@ -97,6 +97,23 @@ static int nostr_log(struct wasm_interp *interp) { return 1; } +static int nostr_set_bool(struct wasm_interp *interp) { + struct val *params = NULL; + const u16 *setting; + u32 val, len; + + if (!get_params(interp, &params, 3) || params == NULL) + return 0; + + if (!mem_ptr_str(interp, params[0].num.i32, (const char**)&setting)) + return 0; + + len = params[1].num.i32; + val = params[2].num.i32 > 0 ? 1 : 0; + + return nscript_set_bool(interp, setting, len, val); +} + static int nostr_pool_send_to(struct wasm_interp *interp) { struct val *params = NULL; const u16 *req, *to; @@ -149,6 +166,7 @@ static struct builtin nscript_builtins[] = { { .name = "nostr_log", .fn = nostr_log }, { .name = "nostr_cmd", .fn = nostr_cmd }, { .name = "nostr_pool_send_to", .fn = nostr_pool_send_to }, + { .name = "nostr_set_bool", .fn = nostr_set_bool }, { .name = "abort", .fn = nscript_abort }, }; diff --git a/damus-c/nostrscript.h b/damus-c/nostrscript.h @@ -18,6 +18,7 @@ int nscript_load(struct wasm_parser *p, struct wasm_interp *interp, unsigned char *wasm, unsigned long len); int nscript_nostr_cmd(struct wasm_interp *interp, int, void*, int); int nscript_pool_send_to(struct wasm_interp *interp, const u16*, int, const u16 *, int); +int nscript_set_bool(struct wasm_interp *interp, const u16*, int, int); #endif /* nostrscript_h */ diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -45,6 +45,7 @@ 4C0A3F8F280F640A000448DE /* ThreadModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F8E280F640A000448DE /* ThreadModel.swift */; }; 4C0A3F93280F66F5000448DE /* ReplyMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C0A3F92280F66F5000448DE /* ReplyMap.swift */; }; 4C190F202A535FC200027FD5 /* CustomizeZapModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C190F1F2A535FC200027FD5 /* CustomizeZapModel.swift */; }; + 4C190F222A53950D00027FD5 /* bool_setting.wasm in Resources */ = {isa = PBXBuildFile; fileRef = 4C190F212A53950D00027FD5 /* bool_setting.wasm */; }; 4C198DEF29F88C6B004C165C /* BlurHashEncode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C198DEB29F88C6B004C165C /* BlurHashEncode.swift */; }; 4C198DF029F88C6B004C165C /* Readme.md in Resources */ = {isa = PBXBuildFile; fileRef = 4C198DEC29F88C6B004C165C /* Readme.md */; }; 4C198DF129F88C6B004C165C /* License.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4C198DED29F88C6B004C165C /* License.txt */; }; @@ -468,6 +469,7 @@ 4C0A3F8E280F640A000448DE /* ThreadModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadModel.swift; sourceTree = "<group>"; }; 4C0A3F92280F66F5000448DE /* ReplyMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyMap.swift; sourceTree = "<group>"; }; 4C190F1F2A535FC200027FD5 /* CustomizeZapModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomizeZapModel.swift; sourceTree = "<group>"; }; + 4C190F212A53950D00027FD5 /* bool_setting.wasm */ = {isa = PBXFileReference; lastKnownFileType = file; name = bool_setting.wasm; path = nostrscript/bool_setting.wasm; sourceTree = SOURCE_ROOT; }; 4C198DEB29F88C6B004C165C /* BlurHashEncode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlurHashEncode.swift; sourceTree = "<group>"; }; 4C198DEC29F88C6B004C165C /* Readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = Readme.md; sourceTree = "<group>"; }; 4C198DED29F88C6B004C165C /* License.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = License.txt; sourceTree = "<group>"; }; @@ -1027,6 +1029,7 @@ 4C4F14AA2A2A76270045A0B9 /* Fixtures */ = { isa = PBXGroup; children = ( + 4C190F212A53950D00027FD5 /* bool_setting.wasm */, 4C4F14AB2A2A763B0045A0B9 /* primal.wasm */, ); path = Fixtures; @@ -1717,6 +1720,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4C190F222A53950D00027FD5 /* bool_setting.wasm in Resources */, 4C4F14AC2A2A763B0045A0B9 /* primal.wasm in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/damus/Models/UserSettingsStore.swift b/damus/Models/UserSettingsStore.swift @@ -15,7 +15,7 @@ let fallback_zap_amount = 1000 private var value: T init(key: String, default_value: T) { - if T is bool { + if T.self == Bool.self { UserSettingsStore.bool_options.insert(key) } self.key = pk_setting_key(UserSettingsStore.pubkey ?? "", key: key) diff --git a/damusTests/NostrScriptTests.swift b/damusTests/NostrScriptTests.swift @@ -18,14 +18,46 @@ final class NostrScriptTests: XCTestCase { // Put teardown code here. This method is called after the invocation of each test method in the class. } - func loadTestWasm() throws -> Data { + func read_bundle_file(name: String, ext: String) throws -> Data { let bundle = Bundle(for: type(of: self)) - guard let fileURL = bundle.url(forResource: "primal", withExtension: "wasm") else { + guard let fileURL = bundle.url(forResource: name, withExtension: ext) else { throw CocoaError(.fileReadNoSuchFile) } return try Data(contentsOf: fileURL) } + + func loadTestWasm() throws -> Data { + return try read_bundle_file(name: "primal", ext: "wasm") + } + + func load_bool_set_test_wasm() throws -> Data { + return try read_bundle_file(name: "bool_setting", ext: "wasm") + } + + func test_bool_set() throws { + var data = try load_bool_set_test_wasm().bytes + let pool = RelayPool() + let script = NostrScript(pool: pool) + let pk = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245" + UserSettingsStore.pubkey = pk + let key = pk_setting_key(pk, key: "nozaps") + UserDefaults.standard.set(true, forKey: key) + + let load_err = script.load(wasm: &data) + XCTAssertNil(load_err) + + let res = script.run() + switch res { + case .finished: + let set = UserDefaults.standard.bool(forKey: key) + XCTAssertEqual(set, false) + case .runtime_err: XCTAssert(false) + case .suspend: + XCTAssert(false) + break + } + } func test_nostrscript() throws { var data = try loadTestWasm().bytes diff --git a/nostrscript/NostrScript.swift b/nostrscript/NostrScript.swift @@ -281,6 +281,23 @@ func nscript_add_relay(script: NostrScript, relay: String) -> Bool { } +@_cdecl("nscript_set_bool") +public func nscript_set_bool(interp: UnsafeMutablePointer<wasm_interp>?, setting: UnsafePointer<UInt16>, setting_len: Int32, val: Int32) -> Int32 { + + guard let setting = asm_str(cstr: setting, len: setting_len), + UserSettingsStore.bool_options.contains(setting) + else { + stack_push_i32(interp, 0); + return 1; + } + + let key = pk_setting_key(UserSettingsStore.pubkey ?? "", key: setting) + UserDefaults.standard.set(val > 0 ? true : false, forKey: key) + + stack_push_i32(interp, 1); + return 1; +} + @_cdecl("nscript_pool_send_to") public func nscript_pool_send_to(interp: UnsafeMutablePointer<wasm_interp>?, preq: UnsafePointer<UInt16>, req_len: Int32, to: UnsafePointer<UInt16>, to_len: Int32) -> Int32 { diff --git a/nostrscript/bool_setting.ts b/nostrscript/bool_setting.ts @@ -0,0 +1,18 @@ + +import * as nostr from './nostr' + +export function go(): i32 { + var setting = "mny`or" + var new_setting = "" + for (let i = 0; i < setting.length; i++) { + new_setting += String.fromCharCode(setting.charCodeAt(i) + 1); + } + // this should fail + if (nostr.set_bool_setting("shmorg", true)) { + // you shouldn't be able to set settings that dont exist + return 0; + } + return nostr.set_bool_setting(new_setting, false) +} + +go() diff --git a/nostrscript/bool_setting.wasm b/nostrscript/bool_setting.wasm Binary files differ. diff --git a/nostrscript/nostr.ts b/nostrscript/nostr.ts @@ -24,6 +24,7 @@ enum Command { declare function nostr_log(log: string): void; declare function nostr_cmd(cmd: i32, val: i32, len: i32): i32; declare function nostr_pool_send_to(req: string, req_len: i32, to: string, to_len: i32): void; +declare function nostr_set_bool(key: string, key_len: i32, val: i32): i32; export function pool_send(req: string): void { nostr_cmd(Command.POOL_SEND, changetype<i32>(req), req.length) @@ -52,6 +53,10 @@ export function event_get_note(ev: Event): Note { return nostr_cmd(Command.EVENT_GET_NOTE, ev, 0) } +export function set_bool_setting(setting: string, value: boolean): i32 { + return nostr_set_bool(setting, setting.length, value) +} + export function note_get_kind(note: Note): u32 { if (!note) return 0; return nostr_cmd(Command.NOTE_GET_KIND, note, 0);