-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fda9e44
commit 73c3fdc
Showing
4 changed files
with
133 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function countPrimes(upTo) { | ||
let count = 0; | ||
const isPrime = Array(upTo + 1).fill(true); | ||
isPrime[0] = isPrime[1] = false; | ||
|
||
for (let i = 2; i * i <= upTo; i++) { | ||
if (isPrime[i]) { | ||
for (let j = i * i; j <= upTo; j += i) { | ||
isPrime[j] = false; | ||
} | ||
} | ||
} | ||
|
||
for (let i = 2; i <= upTo; i++) { | ||
if (isPrime[i]) { | ||
count++; | ||
} | ||
} | ||
|
||
console.log(`Number of primes up to ${upTo}: ${count}`); | ||
} | ||
|
||
countPrimes(100000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use wasmtime::{Engine, Module, Instance, Store, Linker}; | ||
use std::path::Path; | ||
|
||
pub struct WasmExecutor { | ||
engine: Engine, | ||
} | ||
|
||
impl WasmExecutor { | ||
pub fn new() -> Self { | ||
let engine = Engine::default(); | ||
WasmExecutor { engine } | ||
} | ||
|
||
pub fn run(&self, file_path: &str) -> Result<(), String> { | ||
let store = Store::new(&self.engine); | ||
let module = Module::from_file(&self.engine, file_path).map_err(|e| e.to_string())?; | ||
let instance = Instance::new(&store, &module, &[]).map_err(|e| e.to_string())?; | ||
|
||
if let Some(start) = instance.get_func("_start") { | ||
start.call(&[]).map_err(|e| e.to_string())?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |