Skip to content

Instantly share code, notes, and snippets.

View sverhoeven's full-sized avatar

Stefan Verhoeven sverhoeven

View GitHub Profile
@sverhoeven
sverhoeven / div.html
Created October 2, 2020 09:02
run-cpp-on-web: react
<div id="container"></div>
@sverhoeven
sverhoeven / head.html
Created October 2, 2020 09:00
run-cpp-on-web: react
<head>
<title>Example React application</title>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
@sverhoeven
sverhoeven / worker.js
Created October 2, 2020 08:33
run-cpp-on-web: web-worker
importScripts('newtonraphson.js');
onmessage = function(message) {
if (message.data.type === 'CALCULATE') {
createModule().then(({NewtonRaphson}) => {
const tolerance = message.data.payload.tolerance;
const finder = new NewtonRaphson(tolerance);
const initial_guess = message.data.payload.initial_guess;
const root = finder.solve(initial_guess);
postMessage({
@sverhoeven
sverhoeven / example-web-worker.html
Last active October 7, 2020 10:17
run-cpp-on-web: web-worker
<!doctype html>
<html lang="en">
<head>
<title>Web worker example</title>
<script>
const worker = new Worker('worker.js');
worker.postMessage({
type: 'CALCULATE',
payload: { tolerance: 0.001, initial_guess: -4.0 }
});
@sverhoeven
sverhoeven / index.html
Created October 2, 2020 08:20
run-cpp-on-web: webassembly
<html>
<head>
<!-- Load WebAssembly module -->
<script type="text/javascript" src="newtonraphson.js"></script>
</head>
<body>
<div>
Function root is approximately at x =
<span id="answer"/>
</div>
@sverhoeven
sverhoeven / bindings.cpp
Created October 2, 2020 08:19
run-cpp-on-web: webassembly
#include <emscripten/bind.h>
#include "newtonraphson.hpp"
using namespace emscripten;
EMSCRIPTEN_BINDINGS(newtonraphson) {
class_<NewtonRaphson>("NewtonRaphson")
.constructor<float>()
.function("solve", &NewtonRaphson::solve)
;
@sverhoeven
sverhoeven / cli.cpp
Created October 2, 2020 08:19
run-cpp-on-web: webassembly
#include <iostream>
#include <iomanip>
#include "newtonraphson.hpp"
int main() {
float initial_guess = -4;
float tolerance = 0.001;
NewtonRaphson newtonraphson(tolerance);
float root = newtonraphson.solve(initial_guess);
@sverhoeven
sverhoeven / newtonraphson.hpp
Created October 2, 2020 08:17
run-cpp-on-web: webassembly
#ifndef H_NEWTONRAPHSON_HPP
#define H_NEWTONRAPHSON_HPP
class NewtonRaphson {
public:
NewtonRaphson(float tolerance_in);
float solve(float initial_guess);
private:
float tolerance;
};
@sverhoeven
sverhoeven / newtonraphson.cpp
Last active October 7, 2020 11:57
run-cpp-on-web: webassembly
#include "newtonraphson.hpp"
#include "problem.hpp"
#include <cmath>
// Define the constructor method of NewtonRaphson instances
NewtonRaphson::NewtonRaphson(float tolerance_in) : tolerance(tolerance_in) {}
// Define the 'solve' method of NewtonRaphson instances
float NewtonRaphson::solve(float initial_guess) {
float x = initial_guess;
@sverhoeven
sverhoeven / algebra.cpp
Created October 2, 2020 08:16
run-cpp-on-web: webassembly
// An example equation
float equation(float x) {
return 2 * x * x * x - 4 * x * x + 6;
}
// Derivative of the above equation
float derivative(float x) {
return 6 * x * x - 8 * x;
}