Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { build } from "bun";
import { spawnSync } from "child_process";
import { writeFileSync, readFileSync, existsSync, mkdirSync, unlinkSync } from "fs";
import { join, basename } from "path";

async function run() {
const entry = process.argv[2] || "index.ts";
const output = process.argv[3] || "app";

if (!existsSync(entry)) {
console.error(`Entry file ${entry} not found.`);
process.exit(1);
}

console.log("Building AlloyScript bundle...");

const tempEntry = ".alloyscript_entry.ts";
const entryRel = "./" + basename(entry);
const runtimeRel = "./src/runtime.ts";

writeFileSync(tempEntry, `
import "${runtimeRel}";
import userCode from "${entryRel}";
(window as any).defaultExport = userCode;
`);

const result = await build({
entrypoints: [tempEntry],
minify: true,
outdir: "dist",
naming: "bundle.js"
});

unlinkSync(tempEntry);

if (!result.success) {
console.error("Bundle failed", result.logs);
process.exit(1);
}

const bundledJs = readFileSync("dist/bundle.js", "utf-8");

console.log("Generating host bundle...");
const escapedJs = JSON.stringify(bundledJs);
const headerContent = `#ifndef ALLOYSCRIPT_BUNDLE_H\n#define ALLOYSCRIPT_BUNDLE_H\nstatic const char* ALLOYSCRIPT_BUNDLE = ${escapedJs};\n#endif\n`;

if (!existsSync("dist")) mkdirSync("dist");
writeFileSync("dist/bundle.h", headerContent);

console.log("Compiling binary...");

let cflags: string[] = [];
let libs: string[] = [];

try {
cflags = spawnSync("pkg-config", ["--cflags", "gtk+-3.0", "webkit2gtk-4.1"]).stdout.toString().trim().split(/\s+/);
libs = spawnSync("pkg-config", ["--libs", "gtk+-3.0", "webkit2gtk-4.1"]).stdout.toString().trim().split(/\s+/);
} catch (e) {}

const compileArgs = [
"-std=c++11",
"-Icore/include",
"-Idist",
"src/host.cpp",
"core/src/webview.cc",
"-o", output,
"-lutil",
...cflags.filter(s => s.length > 0),
...libs.filter(s => s.length > 0)
];

const compile = spawnSync("c++", compileArgs);

if (compile.status !== 0) {
console.error("Compilation failed");
console.error(compile.stderr.toString());
process.exit(1);
}

console.log(`Success! Binary created: ${output}`);
}

run();
52 changes: 52 additions & 0 deletions core/include/webview/detail/base64.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef WEBVIEW_DETAIL_BASE64_HH
#define WEBVIEW_DETAIL_BASE64_HH

#include <string>
#include <vector>

namespace webview {
namespace detail {

static const char* base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

static inline std::string base64_encode(const std::string& in) {
std::string out;
int val = 0, valb = -6;
for (unsigned char c : in) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
out.push_back(base64_chars[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
while (out.size() % 4) out.push_back('=');
return out;
}

static inline std::string base64_decode(const std::string& in) {
std::vector<int> T(256, -1);
for (int i = 0; i < 64; i++) T[base64_chars[i]] = i;

std::string out;
int val = 0, valb = -8;
for (unsigned char c : in) {
if (T[c] == -1) break;
val = (val << 6) + T[c];
valb += 6;
if (valb >= 0) {
out.push_back(char((val >> valb) & 0xFF));
valb -= 8;
}
}
return out;
}

} // namespace detail
} // namespace webview

#endif // WEBVIEW_DETAIL_BASE64_HH
Loading