Skip to content

Commit 718e750

Browse files
committed
client/server/shared: Add support for JS bindings
1 parent d49af4d commit 718e750

13 files changed

+62
-22
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated
2+
*.gen

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
"format": "cpp",
7777
"forward_list": "cpp",
7878
"optional": "cpp",
79-
"stop_token": "cpp"
79+
"stop_token": "cpp",
80+
"*.gen": "cpp"
8081
},
8182
"editor.defaultFormatter": "xaver.clang-format",
8283
"editor.formatOnSave": true

client/.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ tools/extra.bat
1313
deps/v8/include/
1414
deps/v8/lib/
1515
deps/v8/update.json
16-
17-
# Generated
18-
19-
src/bootstrap.js.gen

client/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 3.19)
22
include(cmake/GitUtils.cmake)
33
include(FetchContent)
44
include(cmake/DepsDownload.cmake)
5+
include(../shared/cmake/GenerateBindings.cmake)
56

67
project(altv-client-js)
78

@@ -41,14 +42,8 @@ endmacro()
4142
GroupSources(${PROJECT_SOURCE_DIR}/src "Source Files")
4243
GroupSources("../shared" "Shared Files")
4344

44-
function(make_includable input_file output_file)
45-
file(READ ${input_file} content)
46-
set(delim "for_c++_include")
47-
set(content "R\"${delim}(\n${content})${delim}\"")
48-
file(WRITE ${output_file} "${content}")
49-
endfunction(make_includable)
50-
5145
make_includable("src/bootstrap.js" "src/bootstrap.js.gen")
46+
GenerateBindings()
5247

5348
include_directories(
5449
${ALTV_JS_CPP_SDK}

client/src/CV8Resource.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
#include "workers/CWorker.h"
2929

30+
#include "JSBindings.h"
31+
3032
extern void StaticRequire(const v8::FunctionCallbackInfo<v8::Value>& info)
3133
{
3234
v8::Isolate* isolate = info.GetIsolate();
@@ -136,6 +138,7 @@ bool CV8ResourceImpl::Start()
136138

137139
ctx->Global()->Set(ctx, V8Helpers::JSValue("__internal_get_exports"), v8::Function::New(ctx, &StaticRequire).ToLocalChecked());
138140
ctx->Global()->Set(ctx, V8Helpers::JSValue("__internal_main_path"), V8Helpers::JSValue(path));
141+
ctx->Global()->Set(ctx, V8Helpers::JSValue("__internal_bindings_code"), V8Helpers::JSValue(JSBindings::GetBindingsCode()));
139142

140143
bool res = curModule->InstantiateModule(ctx, CV8ScriptRuntime::ResolveModule).IsJust();
141144

client/src/bootstrap.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
// clang-format off
2-
await import(`/${__internal_main_path}`);
2+
import * as alt from "alt-client";
3+
4+
// Load the global bindings code
5+
new Function("alt", __internal_bindings_code)(alt);
6+
7+
let mainPath = __internal_main_path;
8+
if(mainPath[0] !== "/") mainPath = "/" + mainPath;
9+
await import(mainPath);

server/.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,4 @@ __pycache__/
298298
.vscode
299299
dist/*
300300

301-
src/bootstrap.js.gen
302-
303301
deps/nodejs/lib/

server/CMakeLists.txt

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required (VERSION 2.9)
22

33
include(../shared/deps/cpp-sdk/CMakeLists.txt)
44
include(cmake/DepsDownload.cmake)
5+
include(../shared/cmake/GenerateBindings.cmake)
56

67
project(js-module)
78

@@ -61,14 +62,8 @@ endmacro()
6162
GroupSources(${PROJECT_SOURCE_DIR}/src "Source Files")
6263
GroupSources("../shared" "Shared Files")
6364

64-
function(make_includable input_file output_file)
65-
file(READ ${input_file} content)
66-
set(delim "for_c++_include")
67-
set(content "R\"${delim}(\n${content})${delim}\"")
68-
file(WRITE ${output_file} "${content}")
69-
endfunction(make_includable)
70-
7165
make_includable("src/bootstrap.js" "src/bootstrap.js.gen")
66+
GenerateBindings()
7267

7368
include_directories(
7469
src

server/src/CNodeResourceImpl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "V8Module.h"
66
#include "V8Helpers.h"
77

8+
#include "JSBindings.h"
9+
810
static void ResourceLoaded(const v8::FunctionCallbackInfo<v8::Value>& info)
911
{
1012
V8_GET_ISOLATE_CONTEXT();
@@ -38,6 +40,7 @@ bool CNodeResourceImpl::Start()
3840
v8::Context::Scope scope(_context);
3941

4042
_context->Global()->Set(_context, V8Helpers::JSValue("__resourceLoaded"), v8::Function::New(_context, &ResourceLoaded).ToLocalChecked());
43+
_context->Global()->Set(_context, V8Helpers::JSValue("__internal_bindings_code"), V8Helpers::JSValue(JSBindings::GetBindingsCode()));
4144

4245
_context->SetAlignedPointerInEmbedderData(1, resource);
4346
context.Reset(isolate, _context);

server/src/bootstrap.js

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
},
4848
});
4949

50+
// Load the global bindings code
51+
new Function("alt", __internal_bindings_code)(alt);
52+
5053
// Get the path to the main file for this resource, and load it
5154
const _path = path.resolve(alt.getResourcePath(alt.resourceName), alt.getResourceMain(alt.resourceName));
5255
_exports = await loader.import("file://" + _path, "", {});

shared/JSBindings.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
5+
static std::string vector3Bindings =
6+
#include "bindings/Vector3.js.gen"
7+
;
8+
9+
namespace JSBindings
10+
{
11+
const std::string& GetBindingsCode()
12+
{
13+
static std::vector<std::string> bindings = { vector3Bindings };
14+
static std::string code;
15+
16+
// Append all bindings to one big bindings module once
17+
if(code.empty())
18+
for(const std::string& binding : bindings) code += binding;
19+
20+
return code;
21+
}
22+
} // namespace JSBindings

shared/bindings/Vector3.js

Whitespace-only changes.

shared/cmake/GenerateBindings.cmake

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Generates the header files for the JavaScript bindings
2+
3+
function(make_includable input_file output_file)
4+
file(READ ${input_file} content)
5+
set(delim "for_c++_include")
6+
set(content "R\"${delim}(\n${content})${delim}\"")
7+
file(WRITE ${output_file} "${content}")
8+
endfunction(make_includable)
9+
10+
function(GenerateBindings)
11+
file(GLOB_RECURSE BINDING_FILES "${PROJECT_SOURCE_DIR}/../shared/bindings/*.js")
12+
foreach(file IN LISTS BINDING_FILES)
13+
make_includable(${file} "${file}.gen")
14+
endforeach()
15+
endfunction()

0 commit comments

Comments
 (0)