Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose runScriptString public method #195

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
3 changes: 3 additions & 0 deletions NativeScript/NativeScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@interface NativeScript : NSObject

- (instancetype)initWithConfig:(Config*)config;

- (void)runScriptString: (NSString*) script runLoop: (BOOL) runLoop;

/**
WARNING: this method does not return in most applications. (UIApplicationMain)
*/
Expand Down
33 changes: 28 additions & 5 deletions NativeScript/NativeScript.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,28 @@ @implementation Config

@implementation NativeScript

extern char defaultStartOfMetadataSection __asm("section$start$__DATA$__TNSMetadata");


std::unique_ptr<Runtime> runtime_;

- (instancetype)initWithConfig:(Config*)config {

if (self = [super init]) {
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
if (config.ApplicationPath != nil) {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
if (config.BaseDir != nil) {
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
if (config.ApplicationPath != nil) {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
} else {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
}
}
if (config.MetadataPtr != nil) {
RuntimeConfig.MetadataPtr = [config MetadataPtr];
} else {
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
RuntimeConfig.MetadataPtr = &defaultStartOfMetadataSection;

}
RuntimeConfig.MetadataPtr = [config MetadataPtr];
RuntimeConfig.IsDebug = [config IsDebug];
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];

Expand Down Expand Up @@ -64,6 +74,19 @@ - (instancetype)initWithConfig:(Config*)config {

}

- (void)runScriptString: (NSString*) script runLoop: (BOOL) runLoop {

std::string cppString = std::string([script UTF8String]);
runtime_->RunScript(cppString);

if (runLoop) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}

tns::Tasks::Drain();

}

- (void)runMainApplication {
runtime_->RunMainScript();

Expand Down
Binary file added NativeScript/metadata/metadata-arm64.bin
Binary file not shown.
Binary file added NativeScript/metadata/metadata-x86_64.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions NativeScript/runtime/ModuleInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ModuleInternal {
public:
ModuleInternal(v8::Local<v8::Context> context);
bool RunModule(v8::Isolate* isolate, std::string path);
void RunScript(v8::Isolate* isolate, std::string script);

private:
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& info);
v8::Local<v8::Function> GetRequireFunction(v8::Isolate* isolate, const std::string& dirName);
Expand All @@ -18,6 +20,7 @@ class ModuleInternal {
v8::Local<v8::String> WrapModuleContent(v8::Isolate* isolate, const std::string& path);
v8::Local<v8::Object> LoadModule(v8::Isolate* isolate, const std::string& modulePath, const std::string& cacheKey);
v8::Local<v8::Object> LoadData(v8::Isolate* isolate, const std::string& modulePath);
v8::MaybeLocal<v8::Value> RunScriptString(v8::Isolate* isolate, v8::Local<v8::Context> context, const std::string script);
std::string ResolvePath(v8::Isolate* isolate, const std::string& baseDir, const std::string& moduleName);
std::string ResolvePathFromPackageJson(const std::string& packageJson, bool& error);
v8::ScriptCompiler::CachedData* LoadScriptCache(const std::string& path);
Expand Down
20 changes: 20 additions & 0 deletions NativeScript/runtime/ModuleInternal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@
return success;
}

void ModuleInternal::RunScript(Isolate* isolate, std::string script) {
std::shared_ptr<Caches> cache = Caches::Get(isolate);
Local<Context> context = cache->GetContext();
Local<Object> globalObject = context->Global();
Local<Value> requireObj;
bool success = globalObject->Get(context, ToV8String(isolate, "require")).ToLocal(&requireObj);
tns::Assert(success && requireObj->IsFunction(), isolate);
Local<Value> result;
this->RunScriptString(isolate, context, script);
}

MaybeLocal<Value> ModuleInternal::RunScriptString(Isolate* isolate, Local<Context> context, const std::string scriptString) {
ScriptCompiler::CompileOptions options = ScriptCompiler::kNoCompileOptions;
ScriptCompiler::Source source(tns::ToV8String(isolate, scriptString));
TryCatch tc(isolate);
Local<Script> script = ScriptCompiler::Compile(context, &source, options).ToLocalChecked();
MaybeLocal<Value> result = script->Run(context);
return result;
}

Local<v8::Function> ModuleInternal::GetRequireFunction(Isolate* isolate, const std::string& dirName) {
Local<v8::Function> requireFuncFactory = requireFactoryFunction_->Get(isolate);
Local<Context> context = isolate->GetCurrentContext();
Expand Down
2 changes: 2 additions & 0 deletions NativeScript/runtime/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Runtime {
}

void RunModule(const std::string moduleName);

void RunScript(const std::string script);

static void Initialize();

Expand Down
9 changes: 9 additions & 0 deletions NativeScript/runtime/Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,15 @@
return std::find(Runtime::isolates_.begin(), Runtime::isolates_.end(), isolate) != Runtime::isolates_.end();
}

void Runtime::RunScript(const std::string script) {
Isolate* isolate = this->GetIsolate();
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
this->moduleInternal_->RunScript(isolate, script);
}


std::shared_ptr<Platform> Runtime::platform_;
std::vector<Isolate*> Runtime::isolates_;
bool Runtime::v8Initialized_ = false;
Expand Down
26 changes: 26 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "NativeScript",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "NativeScript",
targets: ["NativeScript"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.binaryTarget(
name: "NativeScript",
url: "https://github.com/NativeScript/ios-v8-pod/releases/download/spm-test/NativeScript.xcframework.zip",
checksum: "5c6a41ec023b26408ffb6474c6c748bc447958da6a08c9da68138d4841bfb5fe")
]
)
6 changes: 6 additions & 0 deletions build_nativescript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ xcodebuild archive -project v8ios.xcodeproj \
$QUIET \
EXCLUDED_ARCHS="x86_64" \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES \
INCLUDE_DEFAULT_METADATA=$INCLUDE_DEFAULT_METADATA\
-archivePath $DIST/intermediates/NativeScript.maccatalyst.xcarchive
fi

Expand Down Expand Up @@ -104,6 +106,8 @@ xcodebuild archive -project v8ios.xcodeproj \
EXCLUDED_ARCHS="i386" \
DEVELOPMENT_TEAM=$DEV_TEAM \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES \
INCLUDE_DEFAULT_METADATA=$INCLUDE_DEFAULT_METADATA\
-archivePath $DIST/intermediates/NativeScript.iphonesimulator.xcarchive
fi

Expand All @@ -118,6 +122,8 @@ xcodebuild archive -project v8ios.xcodeproj \
EXCLUDED_ARCHS="armv7" \
DEVELOPMENT_TEAM=$DEV_TEAM \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES \
INCLUDE_DEFAULT_METADATA=$INCLUDE_DEFAULT_METADATA\
-archivePath $DIST/intermediates/NativeScript.iphoneos.xcarchive

#Create fat library for simulator
Expand Down
39 changes: 35 additions & 4 deletions v8ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,7 @@
C2DDEB2F229EAB3B00345BFE /* Frameworks */,
C2DDEB30229EAB3B00345BFE /* Resources */,
65A6D684291F63A30074DF31 /* ShellScript */,
2BD4BB6829C0E4720087459A /* Include default metadata */,
);
buildRules = (
);
Expand Down Expand Up @@ -1709,6 +1710,7 @@
};
C2DDEB31229EAB3B00345BFE = {
CreatedOnToolsVersion = 10.2.1;
LastSwiftMigration = 1420;
};
C2F4CBB222C60BFD0036B56F = {
CreatedOnToolsVersion = 10.2.1;
Expand Down Expand Up @@ -1782,6 +1784,24 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
2BD4BB6829C0E4720087459A /* Include default metadata */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Include default metadata";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "echo \"Checking flags to determine whether to copy default metadata\"\nif [ \"$INCLUDE_DEFAULT_METADATA\" = true ] ; then\necho \"Copying ${SRCROOT}/${PRODUCT_NAME}/metadata/metadata-arm64.bin directory to ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework\"\ncp ${SRCROOT}/${PRODUCT_NAME}/metadata/metadata-arm64.bin ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/metadata-arm64.bin\necho \"metadata-arm64.bin has been copied.\"\necho \"Copying ${SRCROOT}/${PRODUCT_NAME}/metadata/metadata-x86_64.bin directory to ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework\"\ncp ${SRCROOT}/${PRODUCT_NAME}/metadata/metadata-x86_64.bin ${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/metadata-x86_64.bin\necho \"metadata-x86_64.bin has been copied.\"\nelse\necho \"Skipped copying metadata binaries.\"\nfi\n\n";
};
65A6D684291F63A30074DF31 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -2616,10 +2636,10 @@
ALLOW_TARGET_PLATFORM_SPECIALIZATION = NO;
BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
CLANG_ENABLE_MODULES = NO;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = NO;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = NO;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -2665,6 +2685,10 @@
"-lv8_base_without_compiler",
"-lv8_compiler",
"-lv8_bigint",
"-sectcreate",
__DATA,
__TNSMetadata,
"$(SRCROOT)/NativeScript/metadata/metadata-x86_64.bin",
"-lv8_snapshot",
"-lcppgc_base",
"-ltorque_generated_initializers",
Expand All @@ -2678,6 +2702,8 @@
PRODUCT_BUNDLE_IDENTIFIER = org.nativescript.NativeScript;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
USER_HEADER_SEARCH_PATHS = (
"$(SRCROOT)/NativeScript",
Expand All @@ -2697,10 +2723,10 @@
ALLOW_TARGET_PLATFORM_SPECIALIZATION = NO;
BUILD_LIBRARY_FOR_DISTRIBUTION = YES;
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
CLANG_ENABLE_MODULES = NO;
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = NO;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = NO;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -2744,6 +2770,10 @@
"-lv8_base_without_compiler",
"-lv8_compiler",
"-lv8_bigint",
"-sectcreate",
__DATA,
__TNSMetadata,
"$(SRCROOT)/NativeScript/metadata/metadata-x86_64.bin",
"-lv8_snapshot",
"-lcppgc_base",
"-ltorque_generated_initializers",
Expand All @@ -2757,6 +2787,7 @@
PRODUCT_BUNDLE_IDENTIFIER = org.nativescript.NativeScript;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
USER_HEADER_SEARCH_PATHS = (
"$(SRCROOT)/NativeScript",
Expand Down