Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
fixed calls to unwrap new Result methods
Browse files Browse the repository at this point in the history
  • Loading branch information
scarletquasar committed Sep 25, 2023
1 parent e6d775c commit 7d34e07
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Bridge implements OutputFriendly {
private itemNames: string[];

constructor(name?: string) {
name ??= newUuid();
name ??= newUuid().unwrap();
_$internalBinding["CreateRealm"](name);
}

Expand Down
12 changes: 7 additions & 5 deletions projects/core/logic/api/modules/interop/interop-core.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DotnetFetchExpression, InteropMethod } from "types/internal/dotnet-interop-types";
import { Result } from "../stdlib/functional-core";

function getExpParts(expression: string) {
return expression.split(":");
}

function getStaticMethod<T>(expression: DotnetFetchExpression): InteropMethod<T> {
function getStaticMethod<T>(expression: DotnetFetchExpression): Result<Error, InteropMethod<T>> {
const parts = getExpParts(expression);
const method = (...args: unknown[]) => {
return _$internalBinding["CallStaticMethod"](
Expand All @@ -15,22 +16,23 @@ function getStaticMethod<T>(expression: DotnetFetchExpression): InteropMethod<T>
);
}

return method;
return Result.right(method);
}

function getStaticProperty<T>(expression: DotnetFetchExpression): T {
function getStaticProperty<T>(expression: DotnetFetchExpression): Result<Error, T> {
const parts = getExpParts(expression);
const property = _$internalBinding["GetStaticProperty"](
parts[0],
parts[1],
parts[2]
);

return property;
return Result.right(property);
}

function loadAssembly(path: string) {
function loadAssembly(path: string): Result<Error, []> {
_$internalBinding["LoadAssembly"](path);
return Result.right([]);
}

export {
Expand Down
19 changes: 8 additions & 11 deletions projects/core/logic/api/modules/server/server-basic-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import { interopCache } from "logic/runtime/interop-cache-core";
import { Result } from "../stdlib/functional-core";
import { newUuid } from "../stdlib/encryption-core";

function customResponse(response: any, type: `${string}/${string}`, headers: Record<string, any> = {}) {
const serialize = getStaticMethod("Newtonsoft.Json:JsonConvert:SerializeObject");
function customResponse(response: any, type: `${string}/${string}`, headers: Record<string, any> = {}) {;
return {
status: 200,
response,
headers: serialize({
"Content-Type": type,
...headers
})
}).unwrap()
}
}

Expand Down Expand Up @@ -68,14 +67,14 @@ function createHost(options = {
host: "0.0.0.0",
port: 80,
enableHttps: false
}): HttpApplication {
const name = newUuid();
}): Result<Error, HttpApplication> {
const name = newUuid().unwrap();
const host = options.host ?? "0.0.0.0";
const port = options.port ?? 80;
const enableHttps = options.enableHttps ?? false;

globalThis.internal.webapps[name] = new HttpApplication(name, host, port, enableHttps);
return globalThis.internal.webapps[name];
return Result.right(globalThis.internal.webapps[name]);
}

function objectResponse<T>(statusCode: number, response: any = {}, headers: Record<string, any> = {}) {
Expand All @@ -94,12 +93,10 @@ class HttpResult<T> {
public response: string;
public headers: string;

private serialize = getStaticMethod<string>("Newtonsoft.Json:JsonConvert:SerializeObject");

constructor(status: number, response: T, headers: Record<string, any>) {
this.status = status;
this.response = this.serialize(response);
this.headers = this.serialize(headers);
this.response = serialize(response).unwrap();
this.headers = serialize(headers).unwrap();
}

useCors(options: CorsOptions) {
Expand All @@ -109,7 +106,7 @@ class HttpResult<T> {
headers["Access-Control-Request-Methods"] = options.methods ? options.methods.toString() : "*";
headers["Access-Control-Allow-Origin"] = options.origin ? options.origin : "*";

this.headers = this.serialize(headers);
this.headers = serialize(headers).unwrap();
}
}

Expand Down
22 changes: 11 additions & 11 deletions projects/core/logic/runtime/interop-cache-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { UUID } from "types/internal/generic-types";

const interopCache = {
serialization: {
serialize: getStaticMethod<string>("System.Text.Json:JsonSerializer:Serialize")
serialize: getStaticMethod<string>("System.Text.Json:JsonSerializer:Serialize").unwrap()
},
console: {
write: getStaticMethod<void>("System:Console:WriteLine"),
clear: getStaticMethod<void>("System:Console:Clear"),
read: getStaticMethod<string | null>("System:Console:ReadLine")
write: getStaticMethod<void>("System:Console:WriteLine").unwrap(),
clear: getStaticMethod<void>("System:Console:Clear").unwrap(),
read: getStaticMethod<string | null>("System:Console:ReadLine").unwrap()
},
io: {
getFiles: getStaticMethod<string[]>("System.IO:Directory:GetFiles")
getFiles: getStaticMethod<string[]>("System.IO:Directory:GetFiles").unwrap()
},
environment: {
getCommandLineArgs: getStaticMethod<string[]>("System:Environment:GetCommandLineArgs"),
currentDirectory: getStaticProperty<string>("System:Environment:CurrentDirectory"),
getEnvironmentVariables: getStaticMethod<Record<string, any>>("System:Environment:GetEnvironmentVariables"),
getEnvironmentVariable: getStaticMethod<string>("System:Environment:GetEnvironmentVariable"),
setEnvironmentVariable: getStaticMethod<void>("System:Environment:SetEnvironmentVariable")
getCommandLineArgs: getStaticMethod<string[]>("System:Environment:GetCommandLineArgs").unwrap(),
currentDirectory: getStaticProperty<string>("System:Environment:CurrentDirectory").unwrap(),
getEnvironmentVariables: getStaticMethod<Record<string, any>>("System:Environment:GetEnvironmentVariables").unwrap(),
getEnvironmentVariable: getStaticMethod<string>("System:Environment:GetEnvironmentVariable").unwrap(),
setEnvironmentVariable: getStaticMethod<void>("System:Environment:SetEnvironmentVariable").unwrap()
},
process: {
exit: _$internalBinding["ProcessExit"],
getCurrentProcess: getStaticMethod<any>("System.Diagnostics:Process:GetCurrentProcess")
getCurrentProcess: getStaticMethod<any>("System.Diagnostics:Process:GetCurrentProcess").unwrap()
},
guid: {
newGuid: _$internalBinding["NewGuid"]
Expand Down
2 changes: 1 addition & 1 deletion projects/native/MelonRuntime.Core/Scripts/core.js

Large diffs are not rendered by default.

0 comments on commit 7d34e07

Please sign in to comment.