Skip to content

Commit 818ebd7

Browse files
author
karurochari
committed
Module structure refactored. More flexible modules
1 parent f57db7e commit 818ebd7

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ cmake_minimum_required(VERSION 3.14)
22

33
project(tjs LANGUAGES C)
44

5+
if(IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/deps/extras")
6+
enable_language(CXX)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
set(CMAKE_CXX_EXTENSIONS ON)
9+
set(CMAKE_CXX_STANDARD 20)
10+
endif()
11+
512
include(ExternalProject)
613
include(GNUInstallDirs)
714

@@ -16,6 +23,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
1623
set(CMAKE_C_EXTENSIONS ON)
1724
set(CMAKE_C_STANDARD 11)
1825

26+
27+
1928
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2029

2130
list(APPEND tjs_cflags -Wall -g)
@@ -72,7 +81,7 @@ endif()
7281

7382
find_package(CURL REQUIRED)
7483

75-
file(GLOB src_extras "src/extras/*.c")
84+
file(GLOB_RECURSE src_extras "src/extras/**/*.c" "src/extras/**/*.cpp")
7685
file(GLOB src_bundles_extras "src/bundles/c/extras/*.c")
7786

7887
add_library(tjs STATIC

extras-helper.mjs

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/env node
22
import { existsSync, write } from 'node:fs'
33
import { readFile, writeFile, mkdir, rm, cp } from 'node:fs/promises'
4+
import { dirname} from 'node:path'
45

56
import { program } from 'commander';
67
import { randomUUID } from 'node:crypto';
@@ -13,20 +14,35 @@ const exec = util.promisify(_exec);
1314
import fg from 'fast-glob'
1415

1516
async function copy_template(path, subdir) {
16-
const tests = await fg(`./extras/${path}/${subdir}/*.js`);
17+
const files = await fg(`./extras/${path}/${subdir}/*.js`);
1718
const prefix = `./${subdir}/${path}/${subdir}/`.length
1819
const suffix = ".js".length
19-
for (const test of tests) {
20-
const name = test.substring(prefix, test.length - suffix).replaceAll("[module]", path)
21-
await writeFile(`./${subdir}/extras/${name}.js`, ((await readFile(test)).toString().replaceAll('__MODULE__', path)))
20+
for (const file of files) {
21+
const name = file.substring(prefix, file.length - suffix).replaceAll("[module]", path)
22+
await writeFile(`./${subdir}/extras/${name}.js`, ((await readFile(file)).toString().replaceAll('__MODULE__', path)))
2223

2324
}
2425
}
2526

2627
async function install(path) {
27-
await writeFile(`./src/extras/${path}.c`, ((await readFile(`./extras/${path}/src/[module].c`)).toString().replaceAll('__MODULE__', path)))
28-
await writeFile(`./src/js/extras/${path}.js`, ((await readFile(`./extras/${path}/src/[module].js`)).toString().replaceAll('__MODULE__', path)))
29-
await writeFile(`./docs/types/extras/${path}.d.ts`, ((await readFile(`./extras/${path}/src/[module].d.ts`)).toString().replaceAll('__MODULE__', path)))
28+
await mkdir(`src/extras/${path}`, { errorOnExist: false });
29+
30+
//Copy over all files in src
31+
{
32+
const files = await fg(`./extras/${path}/src/**/*`);
33+
const prefix = `./extras/${path}/src/`.length
34+
for (const file of files) {
35+
const name = file.substring(prefix).replaceAll("[module]", path)
36+
const fullPath = `./src/extras/${path}/${name}`
37+
await mkdir(dirname(fullPath), { errorOnExist: false, recursive:true });
38+
await writeFile(fullPath, ((await readFile(file)).toString().replaceAll('__MODULE__', path)))
39+
40+
}
41+
}
42+
43+
//While js/ts files must be already reduced in a bundle by this point.
44+
await writeFile(`./src/js/extras/${path}.js`, ((await readFile(`./extras/${path}/bundle/[module].js`)).toString().replaceAll('__MODULE__', path)))
45+
await writeFile(`./docs/types/extras/${path}.d.ts`, ((await readFile(`./extras/${path}/bundle/[module].d.ts`)).toString().replaceAll('__MODULE__', path)))
3046

3147
await copy_template(path, 'examples')
3248
await copy_template(path, 'benchmarks')
@@ -105,7 +121,7 @@ program.command('clone')
105121

106122
//Construct src/extras.bootstrap to initialize the extra modules
107123
await writeFile('./src/extras-bootstrap.c.frag', Object.keys(config).map(x => `tjs__mod_${x}_init(ctx, ns);`).join('\n'))
108-
await writeFile('./src/extras-proto.c.frag', Object.keys(config).map(x => `void tjs__mod_${x}_init(JSContext *ctx, JSValue ns);`).join('\n'))
124+
await writeFile('./src/extras-headers.c.frag', Object.keys(config).map(x => `#include "./extras/${x}/module.h"`).join('\n'))
109125
await writeFile('./src/extras-bundles.c.frag', Object.keys(config).map(x => `#include "bundles/c/extras/${x}.c"`).join('\n'))
110126
await writeFile('./src/extras-entries.c.frag', Object.keys(config).map(x => `{ "tjs:${x}", tjs__${x}, tjs__${x}_size},`).join('\n'))
111127

src/private.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void tjs__mod_wasm_init(JSContext *ctx, JSValue ns);
7878
void tjs__mod_worker_init(JSContext *ctx, JSValue ns);
7979
void tjs__mod_ws_init(JSContext *ctx, JSValue ns);
8080
void tjs__mod_xhr_init(JSContext *ctx, JSValue ns);
81-
#if __has_include("extras-proto.c.frag")
82-
#include "extras-proto.c.frag"
81+
#if __has_include("extras-headers.c.frag")
82+
#include "extras-headers.c.frag"
8383
#endif
8484

8585
#ifndef _WIN32

0 commit comments

Comments
 (0)