-
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.
Attempt to package rust for the browser
Note that this was done in an environment where I cannot test So a failure is not that unexpected
- Loading branch information
1 parent
53e36c3
commit aef1d70
Showing
8 changed files
with
226 additions
and
56 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
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,73 @@ | ||
<!-- test/support/index.html --> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>JavaScript Tests (Packaged with WebPack + Babel)</title> | ||
|
||
<link href="https://npmcdn.com/[email protected]/mocha.css" rel="stylesheet" /> | ||
<script src="https://npmcdn.com/[email protected]/mocha.js"></script> | ||
</head> | ||
<body> | ||
|
||
<!-- A container element for the visual Mocha results --> | ||
<div id="mocha"></div> | ||
|
||
<!-- Mocha setup and initiation code --> | ||
<script> | ||
mocha.setup('bdd'); | ||
window.onload = function() { | ||
var runner = mocha.run(); | ||
var failedTests = []; | ||
|
||
runner.on('end', function() { | ||
window.mochaResults = runner.stats; | ||
window.mochaResults.reports = failedTests; | ||
}); | ||
|
||
runner.on('fail', logFailure); | ||
|
||
function logFailure(test, err){ | ||
var flattenTitles = function(test){ | ||
var titles = []; | ||
while (test.parent.title){ | ||
titles.push(test.parent.title); | ||
test = test.parent; | ||
} | ||
return titles.reverse(); | ||
}; | ||
|
||
failedTests.push({ | ||
name: test.title, | ||
result: false, | ||
message: err.message, | ||
stack: err.stack, | ||
titles: flattenTitles(test) | ||
}); | ||
}; | ||
}; | ||
</script> | ||
<script type="module"> | ||
import init, { get_problems, run_problem, get_answer } from './dist/rust.js'; | ||
async function runTests() { | ||
await init(); | ||
for (const p of get_problems()) { | ||
const expected = await get_answer(p); | ||
describe(`run test ${p}`, function() { | ||
this.timeout(Infinity); | ||
it(`should return ${expected}`, async () => { | ||
const answer = await run_problem(p); | ||
console.log(p, answer, expected); | ||
if (answer !== expected) { | ||
throw new Error(); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
runTests(); | ||
</script> | ||
|
||
</body> | ||
</html> |
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,68 @@ | ||
use seq_macro::seq; | ||
|
||
pub mod include; | ||
pub use include::utils::{Answer,get_answer}; | ||
seq!(N in 0001..=0020 { | ||
mod p~N; | ||
}); | ||
mod p0022; | ||
mod p0024; | ||
mod p0027; | ||
mod p0034; | ||
mod p0069; | ||
mod p0076; | ||
mod p0077; | ||
mod p0087; | ||
mod p0357; | ||
mod p0836; | ||
|
||
type ProblemType = fn() -> Answer; | ||
type ProblemRef<'a> = (&'a usize, ProblemType, bool); | ||
|
||
fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> { | ||
return match n { | ||
1 => Some(( &1, p0001::p0001, false)), | ||
2 => Some(( &2, p0002::p0002, false)), | ||
3 => Some(( &3, p0003::p0003, false)), | ||
4 => Some(( &4, p0004::p0004, false)), | ||
5 => Some(( &5, p0005::p0005, false)), | ||
6 => Some(( &6, p0006::p0006, false)), | ||
7 => Some(( &7, p0007::p0007, false)), | ||
8 => Some(( &8, p0008::p0008, false)), | ||
9 => Some(( &9, p0009::p0009, false)), | ||
10 => Some(( &10, p0010::p0010, false)), | ||
11 => Some(( &11, p0011::p0011, false)), | ||
12 => Some(( &12, p0012::p0012, false)), | ||
13 => Some(( &13, p0013::p0013, false)), | ||
14 => Some(( &14, p0014::p0014, false)), | ||
15 => Some(( &15, p0015::p0015, false)), | ||
16 => Some(( &16, p0016::p0016, false)), | ||
17 => Some(( &17, p0017::p0017, false)), | ||
18 => Some(( &18, p0018::p0018, false)), | ||
19 => Some(( &19, p0019::p0019, false)), | ||
20 => Some(( &20, p0020::p0020, false)), | ||
22 => Some(( &22, p0022::p0022, false)), | ||
24 => Some(( &24, p0024::p0024, false)), | ||
27 => Some(( &27, p0027::p0027, false)), | ||
34 => Some(( &34, p0034::p0034, false)), | ||
69 => Some(( &69, p0069::p0069, false)), | ||
76 => Some(( &76, p0076::p0076, false)), | ||
77 => Some(( &77, p0077::p0077, false)), | ||
87 => Some(( &87, p0087::p0087, false)), | ||
357 => Some((&357, p0357::p0357, true)), | ||
836 => Some((&836, p0836::p0836, false)), | ||
_ => None, | ||
}; | ||
} | ||
|
||
|
||
pub fn generate_supported_problems() -> Vec<usize> { | ||
let mut supported_problems = Vec::new(); | ||
for n in 1..10000 { | ||
if get_problem(n).is_some() { | ||
supported_problems.push(n); | ||
} | ||
} | ||
|
||
return supported_problems; | ||
} |
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,29 @@ | ||
#[cfg(feature = "wasm")] | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[cfg(feature = "wasm")] | ||
pub use include::problems::{generate_supported_problems,get_problem}; | ||
#[cfg(feature = "wasm")] | ||
pub use include::utils::get_answer; | ||
|
||
#[cfg(feature = "wasm")] | ||
#[wasm_bindgen] | ||
pub fn run_problem(n: usize) -> JsValue { | ||
let (problem_number, problem_function, _) = get_problem(n); | ||
let answer = problem_function(); | ||
JsValue::from_serde(&answer).unwrap_or_else(|err| JsValue::from_str(&format!("Serialization error: {}", err))); | ||
} | ||
|
||
#[cfg(feature = "wasm")] | ||
#[wasm_bindgen] | ||
pub fn get_problems() -> JsValue { | ||
let answer = generate_supported_problems(); | ||
JsValue::from_serde(&answer).unwrap_or_else(|err| JsValue::from_str(&format!("Serialization error: {}", err))); | ||
} | ||
|
||
#[cfg(feature = "wasm")] | ||
#[wasm_bindgen] | ||
pub fn get_answer(n: usize) -> JsValue { | ||
let answer = get_answer(n); | ||
JsValue::from_serde(&answer).unwrap_or_else(|err| JsValue::from_str(&format!("Serialization error: {}", err))); | ||
} |
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