Skip to content

Commit 9440f4c

Browse files
committed
Delete MotelyExports, call real types directly
MotelyExports was 6 proxy one-liners + RunSearch. Moved RunSearch into MotelySearchOrchestrator where it belongs. Both MotelyWasmExports and MotelyNodeExports now call the underlying types directly: MotelySeedAnalyzer, JamlConfigLoader, MotelyBuildVersion, Vector128. One less file, one less indirection layer. https://claude.ai/code/session_019weRHQbABp2dzatqkp2aKV
1 parent 7f4a08f commit 9440f4c

File tree

4 files changed

+93
-176
lines changed

4 files changed

+93
-176
lines changed
Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,85 @@
11
using System.Runtime.InteropServices.JavaScript;
2+
using System.Runtime.Intrinsics;
23
using System.Runtime.Versioning;
34
using System.Text.Json;
45
using Motely;
56
using Motely.Analysis;
67
using Motely.Executors;
8+
using Motely.Filters;
79

810
namespace Motely.BrowserWasm;
911

10-
/// <summary>
11-
/// Browser WASM exports. Thin wrappers around MotelyExports (shared orchestration).
12-
/// Each method adds [JSExport] and async Task.Run so the browser thread isn't blocked.
13-
/// </summary>
1412
[SupportedOSPlatform("browser")]
1513
public static partial class MotelyWasmExports
1614
{
1715
private static CancellationTokenSource? _activeCts;
1816

19-
// ── SEARCH (sequential) ──
17+
// ── SEARCH ──
2018

2119
[JSExport]
2220
public static Task<string> StartJamlSearch(
2321
string jamlContent, int threadCount, int batchCharCount,
2422
int startBatch, int endBatch,
2523
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<long, long, long> onProgress,
2624
[JSMarshalAs<JSType.Function<JSType.String, JSType.Number>>] Action<string, int> onResult)
27-
{
28-
return RunSearch(jamlContent, new MotelySearchRequest
25+
=> RunSearch(jamlContent, new MotelySearchRequest
2926
{
3027
ThreadCount = ResolveThreads(threadCount),
3128
BatchCharCount = ResolveBatch(batchCharCount),
3229
StartBatch = startBatch >= 0 ? startBatch : null,
3330
EndBatch = endBatch > 0 ? endBatch : null,
3431
}, onProgress, onResult);
35-
}
36-
37-
// ── SEARCH (seed list / verify) ──
3832

3933
[JSExport]
4034
public static Task<string> StartSeedListSearch(
4135
string jamlContent, int threadCount, int batchCharCount,
4236
[JSMarshalAs<JSType.Array<JSType.String>>] string[] seeds,
4337
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<long, long, long> onProgress,
4438
[JSMarshalAs<JSType.Function<JSType.String, JSType.Number>>] Action<string, int> onResult)
45-
{
46-
return RunSearch(jamlContent, new MotelySearchRequest
39+
=> RunSearch(jamlContent, new MotelySearchRequest
4740
{
4841
ThreadCount = ResolveThreads(threadCount),
4942
BatchCharCount = ResolveBatch(batchCharCount),
5043
Seeds = seeds,
5144
}, onProgress, onResult);
52-
}
53-
54-
// ── SEARCH (keyword) ──
5545

5646
[JSExport]
5747
public static Task<string> StartKeywordSearch(
5848
string jamlContent, int threadCount, int batchCharCount,
5949
[JSMarshalAs<JSType.Array<JSType.String>>] string[] keywords, string padding,
6050
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<long, long, long> onProgress,
6151
[JSMarshalAs<JSType.Function<JSType.String, JSType.Number>>] Action<string, int> onResult)
62-
{
63-
return RunSearch(jamlContent, new MotelySearchRequest
52+
=> RunSearch(jamlContent, new MotelySearchRequest
6453
{
6554
ThreadCount = ResolveThreads(threadCount),
6655
BatchCharCount = ResolveBatch(batchCharCount),
6756
Keywords = keywords,
6857
Padding = string.IsNullOrEmpty(padding) ? null : padding,
6958
}, onProgress, onResult);
70-
}
71-
72-
// ── SEARCH (random) ──
7359

7460
[JSExport]
7561
public static Task<string> StartRandomSearch(
7662
string jamlContent, int threadCount, int batchCharCount, int count,
7763
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<long, long, long> onProgress,
7864
[JSMarshalAs<JSType.Function<JSType.String, JSType.Number>>] Action<string, int> onResult)
79-
{
80-
return RunSearch(jamlContent, new MotelySearchRequest
65+
=> RunSearch(jamlContent, new MotelySearchRequest
8166
{
8267
ThreadCount = ResolveThreads(threadCount),
8368
BatchCharCount = ResolveBatch(batchCharCount),
8469
RandomSeeds = count > 0 ? count : 1000,
8570
}, onProgress, onResult);
86-
}
87-
88-
// ── SEARCH (palindrome) ──
8971

9072
[JSExport]
9173
public static Task<string> StartPalindromeSearch(
9274
string jamlContent, int threadCount, int batchCharCount,
9375
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<long, long, long> onProgress,
9476
[JSMarshalAs<JSType.Function<JSType.String, JSType.Number>>] Action<string, int> onResult)
95-
{
96-
return RunSearch(jamlContent, new MotelySearchRequest
77+
=> RunSearch(jamlContent, new MotelySearchRequest
9778
{
9879
ThreadCount = ResolveThreads(threadCount),
9980
BatchCharCount = ResolveBatch(batchCharCount),
10081
Palindrome = true,
10182
}, onProgress, onResult);
102-
}
10383

10484
[JSExport]
10585
public static Task StopSearch()
@@ -115,7 +95,7 @@ public static Task<string> AnalyzeSeed(string seed, string deck, string stake)
11595
{
11696
try
11797
{
118-
var dto = MotelyExports.AnalyzeSeed(seed, deck, stake);
98+
var dto = MotelySeedAnalyzer.AnalyzeToDto(seed, deck, stake);
11999
return Task.FromResult(JsonSerializer.Serialize(dto, AnalysisJsonContext.Default.SeedAnalysisDto));
120100
}
121101
catch (Exception ex)
@@ -126,31 +106,33 @@ public static Task<string> AnalyzeSeed(string seed, string deck, string stake)
126106
}
127107
}
128108

129-
// ── SIMPLE GETTERS ──
109+
// ── GETTERS ──
130110

131111
[JSExport]
132-
public static Task<string> GetVersion() => Task.FromResult(MotelyExports.GetVersion(typeof(MotelyCore).Assembly));
112+
public static Task<string> GetVersion() => Task.FromResult(MotelyBuildVersion.For(typeof(MotelyCore).Assembly));
133113

134114
[JSExport]
135-
public static Task<bool> IsSimdEnabled() => Task.FromResult(MotelyExports.IsSimdEnabled());
115+
public static Task<bool> IsSimdEnabled() => Task.FromResult(Vector128.IsHardwareAccelerated);
136116

137117
[JSExport]
138-
public static Task<int> GetProcessorCount() => Task.FromResult(MotelyExports.GetProcessorCount());
118+
public static Task<int> GetProcessorCount() => Task.FromResult(Environment.ProcessorCount);
139119

140120
[JSExport]
141-
public static Task<bool> ValidateJaml(string jamlContent) => Task.FromResult(MotelyExports.ValidateJaml(jamlContent));
121+
public static Task<bool> ValidateJaml(string jamlContent) => Task.FromResult(JamlConfigLoader.TryLoad(jamlContent, out _, out _));
142122

143123
[JSExport]
144-
public static Task<string> ValidateJamlWithError(string jamlContent) => Task.FromResult(MotelyExports.ValidateJamlWithError(jamlContent));
124+
public static Task<string> ValidateJamlWithError(string jamlContent)
125+
{
126+
if (JamlConfigLoader.TryLoad(jamlContent, out _, out var error))
127+
return Task.FromResult("");
128+
return Task.FromResult(error ?? "Unknown validation error");
129+
}
145130

146131
// ── Internals ──
147132

148133
private static int ResolveThreads(int t) => t > 0 ? t : Environment.ProcessorCount;
149134
private static int ResolveBatch(int b) => b is >= 1 and <= 7 ? b : 4;
150135

151-
/// <summary>
152-
/// Delegates to MotelyExports.RunSearch (shared orchestration) with cancellation + async.
153-
/// </summary>
154136
private static async Task<string> RunSearch(
155137
string jamlContent, MotelySearchRequest request,
156138
Action<long, long, long> onProgress, Action<string, int> onResult)
@@ -164,24 +146,11 @@ private static async Task<string> RunSearch(
164146
try
165147
{
166148
var (status, _, _) = await Task.Run(() =>
167-
MotelyExports.RunSearch(jamlContent, request, onProgress, onResult, cts.Token));
149+
MotelySearchOrchestrator.RunSearch(jamlContent, request, onProgress, onResult, cts.Token));
168150
return status;
169151
}
170-
catch (OperationCanceledException)
171-
{
172-
return "cancelled";
173-
}
174-
catch (InvalidOperationException ex)
175-
{
176-
return $"error: {ex.Message}";
177-
}
178-
catch (Exception ex)
179-
{
180-
return $"error: {ex.Message}";
181-
}
182-
finally
183-
{
184-
_activeCts = null;
185-
}
152+
catch (OperationCanceledException) { return "cancelled"; }
153+
catch (Exception ex) { return $"error: {ex.Message}"; }
154+
finally { _activeCts = null; }
186155
}
187156
}

Motely.NodeAddon/MotelyNodeExports.cs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,34 @@
77

88
namespace Motely.NodeAddon;
99

10-
/// <summary>
11-
/// Node.js native addon exports. Thin wrappers around MotelyExports (shared orchestration).
12-
/// node-api-dotnet auto-generates JS bindings for [JSExport].
13-
/// JAMMY's index.cjs loads via require("./Motely.NodeAddon.node").MotelyNodeExports.
14-
/// </summary>
1510
[JSExport]
1611
public static class MotelyNodeExports
1712
{
1813
// ── Capabilities ──
1914

2015
public static CapabilitiesDto GetCapabilities() => new()
2116
{
22-
Simd = MotelyExports.IsSimdEnabled(),
17+
Simd = Vector128.IsHardwareAccelerated,
2318
Threads = true,
24-
AvailableThreadCount = MotelyExports.GetProcessorCount(),
25-
ProcessorCount = MotelyExports.GetProcessorCount(),
19+
AvailableThreadCount = Environment.ProcessorCount,
20+
ProcessorCount = Environment.ProcessorCount,
2621
Runtime = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription,
27-
Version = MotelyExports.GetVersion(typeof(MotelyCore).Assembly),
22+
Version = MotelyBuildVersion.For(typeof(MotelyCore).Assembly),
2823
Timestamp = DateTime.UtcNow.ToString("o"),
2924
};
3025

31-
// ── Seed analysis — delegates to MotelyExports ──
26+
// ── Seed analysis ──
3227

3328
public static SeedAnalysisDto AnalyzeSeed(string seed, string deck, string stake)
3429
{
35-
try { return MotelyExports.AnalyzeSeed(seed, deck, stake); }
30+
try { return MotelySeedAnalyzer.AnalyzeToDto(seed, deck, stake); }
3631
catch (Exception ex)
3732
{
3833
return new SeedAnalysisDto { Seed = seed, Deck = deck, Stake = stake, Error = ex.Message };
3934
}
4035
}
4136

42-
// ── JAML validation — delegates to MotelyExports ──
37+
// ── JAML validation ──
4338

4439
public static ValidateResultDto ValidateJaml(string jamlContent)
4540
{
@@ -55,7 +50,7 @@ public static ValidateResultDto ValidateJaml(string jamlContent)
5550
return new ValidateResultDto { Valid = false, Error = error ?? "Unknown error" };
5651
}
5752

58-
// ── Single-block search (distributed worker pool) — delegates to ProcessBlockRunner ──
53+
// ── Single-block search (distributed worker pool) ──
5954

6055
public static async Task<BlockSearchResultDto> ProcessBlockAsync(string jamlContent, int blockId)
6156
{
@@ -66,75 +61,63 @@ public static async Task<BlockSearchResultDto> ProcessBlockAsync(string jamlCont
6661
return ToDto(result);
6762
}
6863

69-
// ── Full searches — all delegate to MotelyExports.RunSearch ──
64+
// ── Full searches — all delegate to MotelySearchOrchestrator.RunSearch ──
7065

7166
public static BlockSearchResultDto RunSequentialRangeAsync(
7267
string jamlContent, int startBlock, int endBlock)
73-
{
74-
return RunViaShared(jamlContent, new MotelySearchRequest
68+
=> RunViaOrchestrator(jamlContent, new MotelySearchRequest
7569
{
7670
ThreadCount = Environment.ProcessorCount,
7771
BatchCharCount = ProcessBlockRunner.BatchCharCount,
7872
StartBatch = startBlock,
7973
EndBatch = endBlock,
8074
}, startBlock);
81-
}
8275

8376
public static BlockSearchResultDto RunListSearchAsync(
8477
string jamlContent, string[] seeds)
85-
{
86-
return RunViaShared(jamlContent, new MotelySearchRequest
78+
=> RunViaOrchestrator(jamlContent, new MotelySearchRequest
8779
{
8880
ThreadCount = Environment.ProcessorCount,
8981
BatchCharCount = ProcessBlockRunner.BatchCharCount,
9082
Seeds = seeds,
9183
});
92-
}
9384

9485
public static BlockSearchResultDto RunKeywordsSearchAsync(
9586
string jamlContent, string[] keywords, string? padding)
96-
{
97-
return RunViaShared(jamlContent, new MotelySearchRequest
87+
=> RunViaOrchestrator(jamlContent, new MotelySearchRequest
9888
{
9989
ThreadCount = Environment.ProcessorCount,
10090
BatchCharCount = ProcessBlockRunner.BatchCharCount,
10191
Keywords = keywords,
10292
Padding = string.IsNullOrEmpty(padding) ? null : padding,
10393
});
104-
}
10594

10695
public static BlockSearchResultDto RunRandomSearchAsync(
10796
string jamlContent, int count)
108-
{
109-
return RunViaShared(jamlContent, new MotelySearchRequest
97+
=> RunViaOrchestrator(jamlContent, new MotelySearchRequest
11098
{
11199
ThreadCount = Environment.ProcessorCount,
112100
BatchCharCount = ProcessBlockRunner.BatchCharCount,
113101
RandomSeeds = count > 0 ? count : 1000,
114102
});
115-
}
116103

117104
public static BlockSearchResultDto RunPalindromeSearchAsync(string jamlContent)
118-
{
119-
return RunViaShared(jamlContent, new MotelySearchRequest
105+
=> RunViaOrchestrator(jamlContent, new MotelySearchRequest
120106
{
121107
ThreadCount = Environment.ProcessorCount,
122108
BatchCharCount = ProcessBlockRunner.BatchCharCount,
123109
Palindrome = true,
124110
});
125-
}
126111

127112
// ── Private helpers ──
128113

129-
private static BlockSearchResultDto RunViaShared(
114+
private static BlockSearchResultDto RunViaOrchestrator(
130115
string jamlContent, MotelySearchRequest request, int blockId = 0)
131116
{
132-
// Collect at the wrapper level — bounded by JS-side chunking (500 blocks per call).
133-
// The orchestrator streams via callbacks; this is just the final DTO for the JS boundary.
134117
var seeds = new List<string>();
135118
int highestScore = 0;
136119

137-
var (status, seedsFound, _) = MotelyExports.RunSearch(jamlContent, request,
120+
var (status, seedsFound, _) = MotelySearchOrchestrator.RunSearch(jamlContent, request,
138121
onResult: (seed, score) =>
139122
{
140123
seeds.Add(seed);

0 commit comments

Comments
 (0)