Skip to content

Commit

Permalink
Merge pull request #84 from unyt-org/fix-reporting
Browse files Browse the repository at this point in the history
Fix reporting
  • Loading branch information
jonasstrehle authored Feb 12, 2024
2 parents ea13339 + 8b04800 commit 8020736
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
55 changes: 42 additions & 13 deletions js_adapter/js_class_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1150,26 +1150,31 @@ function getMethodParams(target:Function, method_name:string, meta_param_index?:

if (!(method_name in target)) return null;

let tuple = new Tuple();
let metadata:any[] = MetadataReflect.getMetadata && MetadataReflect.getMetadata("design:paramtypes", target, method_name);
const tuple = new Tuple();
const metadata:any[] = MetadataReflect.getMetadata && MetadataReflect.getMetadata("design:paramtypes", target, method_name);

if (!metadata) return null;

// get parmeters names from function body string
const function_body:string = target[method_name]?.toString();
const args_strings = function_body?.match(/^[^(]*\(([^)]*)\)/)?.[1]?.split(",");

if (args_strings) {
for (let i=0;i<args_strings.length;i++) {
args_strings[i] = args_strings[i].trim().split(/[ =]/)[0];
}
const args_match = function_body?.match(/^[^(]*\(([^)]*)\)/)?.[1];

if (args_match) {
const args_strings = normalizeFunctionParams(args_match)?.split(",");

// add type metadata
let i = 0;
for (let arg of args_strings) {
if (meta_param_index != null && meta_param_index == i) {i++; continue} // skip meta param index
tuple.set(arg, metadata[i] ? Type.getClassDatexType(metadata[i]) : Type.std.Any);
i++;
if (args_strings) {
for (let i=0;i<args_strings.length;i++) {
args_strings[i] = args_strings[i].trim().split(/[ =]/)[0];
}

// add type metadata
let i = 0;
for (const arg of args_strings) {
if (meta_param_index != null && meta_param_index == i) {i++; continue} // skip meta param index
tuple.set(arg, metadata[i] ? Type.getClassDatexType(metadata[i]) : Type.std.Any);
i++;
}
}
}

Expand All @@ -1180,6 +1185,30 @@ function getMetaParamIndex(target:Function, method_name:string):number {
(MetadataReflect.getMetadata && MetadataReflect.getMetadata("unyt:meta", target, method_name));
}

// TODO: refactor, merge with parser in function.ts
function normalizeFunctionParams(params: string) {
let scopes = 0;
let nestedIndex = undefined;

let i=0;
let varCount = 0;
for (const x of params) {
if (x === "["||x === "{") scopes++;
if (x === "]"||x === "}") scopes--;
if (nestedIndex == undefined && scopes !== 0) {
nestedIndex = i;
}
else if(nestedIndex != undefined && scopes == 0) {
params = [...params].toSpliced(nestedIndex, i-nestedIndex+1, 'x_'+(varCount++)).join("")
i = nestedIndex
nestedIndex = undefined;
}
i++;
}

return params;
}


// let _assigner_init = false;
// function initPropertyTypeAssigner(){
Expand Down
28 changes: 27 additions & 1 deletion types/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class Function<T extends (...args: any) => any = (...args: any) => any> e
const args_match = function_body?.match(/^[^(]*\(([^)]*)\)/)?.[1];

if (!args_match?.length) return tuple;
const args_strings = args_match.split(",");
const args_strings = this.normalizeFunctionParams(args_match).split(",");
if (args_strings) {
for (let arg of args_strings) {
arg = arg.trim().split(/[ =]/)[0];
Expand All @@ -244,6 +244,32 @@ export class Function<T extends (...args: any) => any = (...args: any) => any> e
return tuple;
}

/**
* Normalizes a function params string, replacing destructuring with a single variable
*/
private static normalizeFunctionParams(params: string) {
let scopes = 0;
let nestedIndex = undefined;

let i=0;
let varCount = 0;
for (const x of params) {
if (x === "["||x === "{") scopes++;
if (x === "]"||x === "}") scopes--;
if (nestedIndex == undefined && scopes !== 0) {
nestedIndex = i;
}
else if(nestedIndex != undefined && scopes == 0) {
params = [...params].toSpliced(nestedIndex, i-nestedIndex+1, 'x_'+(varCount++)).join("")
i = nestedIndex
nestedIndex = undefined;
}
i++;
}

return params;
}


// execute this function remotely, set the endpoint
private setRemoteEndpoint(endpoint: Endpoint){
Expand Down
3 changes: 2 additions & 1 deletion utils/error-reporting.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Compiler } from "../compiler/compiler.ts";
import { sendDatexViaHTTPChannel } from "../network/datex-http-channel.ts";
import { Runtime } from "../runtime/runtime.ts";
import { LOCAL_ENDPOINT } from "../types/addressing.ts";
import { getCallerInfo } from "../utils/caller_metadata.ts";
import { logger } from "./global_values.ts";

Expand All @@ -24,7 +25,7 @@ export async function sendReport(identifier: string, reportData:Record<string,an
}

const dx = `#endpoint.Reporting.sendReport(?)`
const dxb = <ArrayBuffer> await Compiler.compile(dx, [report], {sign: false, encrypt: false});
const dxb = <ArrayBuffer> await Compiler.compile(dx, [report], {sign: false, encrypt: false, to: LOCAL_ENDPOINT});
sendDatexViaHTTPChannel(dxb, "https://status.unyt.org")
}

Expand Down

0 comments on commit 8020736

Please sign in to comment.