sebastienros / jint

Javascript Interpreter for .NET
BSD 2-Clause "Simplified" License
4.02k stars 556 forks source link

System.NullReferenceException: Object reference not set to an instance of an object. #1908

Closed lucidium4 closed 1 month ago

lucidium4 commented 1 month ago

Version used

3.1.3

Describe the bug

Calling a function in another object with args Triggers a NullReferenceException

   at Jint.Runtime.Interpreter.Expressions.JintIdentifierExpression.GetValue(EvaluationContext context)
   at Jint.Runtime.Environments.FunctionEnvironment.HandleAssignmentPatternOrExpression(EvaluationContext context, Node left, Node right, JsValue argument, Boolean initiallyEmpty)
   at Jint.Runtime.Environments.FunctionEnvironment.SetFunctionParameterUnlikely(EvaluationContext context, Node parameter, JsValue[] arguments, Int32 index, Boolean initiallyEmpty)
   at Jint.Runtime.Environments.FunctionEnvironment.AddFunctionParameters(EvaluationContext context, IFunction functionDeclaration, JsValue[] arguments)
   at Jint.Engine.FunctionDeclarationInstantiation(Function function, JsValue[] argumentsList)
   at Jint.Native.Function.ScriptFunction.Jint.Native.IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
   at Jint.Engine.Construct(Function function, JsValue[] arguments, JsValue newTarget, JintExpression expression)
   at Jint.Engine.Construct(JsValue constructor, JsValue[] arguments, JsValue newTarget, JintExpression expression)
   at Jint.Runtime.Interpreter.Expressions.JintNewExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintVariableDeclaration.ExecuteInternal(EvaluationContext context)
   at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)
   at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.GetValue(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintReturnStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)
   at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintExpressionStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteSingle(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteBlock(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintIfStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)
   at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintExpressionStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteBlock(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintIfStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteBlock(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintForInForOfStatement.BodyEvaluation(EvaluationContext context, JintExpression lhs, ProbablyBlockStatement& stmt, IteratorInstance iteratorRecord, IterationKind iterationKind, LhsKind lhsKind, IteratorKind iteratorKind)
   at Jint.Runtime.Interpreter.Statements.JintForInForOfStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteSingle(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintBlockStatement.ExecuteBlock(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintIfStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)
   at Jint.Runtime.Interpreter.Expressions.JintCallExpression.EvaluateInternal(EvaluationContext context)
   at Jint.Runtime.Interpreter.Statements.JintExpressionStatement.ExecuteInternal(EvaluationContext context)
   at Jint.Native.Function.ScriptFunction.Call(JsValue thisObject, JsValue[] arguments)
   at lambda_method9(Closure, Int64)
   at Godot.ItemList.ItemActivatedTrampoline(Object delegateObj, NativeVariantPtrArgs args, godot_variant& ret) in /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Generated/GodotObjects/ItemList.cs:line 1096}

To Reproduce

REDACTED

Expected behavior

A clear and concise description of what you expected to happen.

The expect behavior would be Once the file is clicked A Widget should Open In Tabed Area in the Center.

Additional context

I'm current writing the editor for my game engine in JavaScript Running on top of Jint. The Editor is Designed with a modular approach using MVC-based Widgets

Add any other context about the problem here. no.

lahma commented 1 month ago

Can you narrow this down to a smaller test case?

lucidium4 commented 1 month ago

Here's the code thats causing the exceptions

javaScriptHandler.js

import FileHandler from "assets://widgets/dock/explorer/fileHandler.js";

export default class JavaScriptHandler extends FileHandler {
    extension = ".js";
    icon = "assets://FugueIcons/icons/script-code.png";

    openFile(path) {
        if (path == null || path == undefined) {
            console.error("Path is null or undefined");
            return;
        }
        console.log("Opening JavaScript file: " + path);
        if (this.core == null || this.core == undefined) {
            console.error("Core is null or undefined");
            return;
        }
        let fileName;
        if (path) {
            fileName = path.split("/").pop()
        }
        console.log
        if (this.explorer != null || this.explorer != undefined) {
            this.core.controller.loadPreloadedWidgetToWorkspace('scriptEditor');
        }
    }

}

/core/controller.js

loadPreloadedWidget(widgetName, rootNode){
        const preloadedWidget = this.model.preloadedWidgets[widgetName];
        const model = preloadedWidget.model;
        const view = preloadedWidget.view;
        const controller = preloadedWidget.controller;
        const widget = new WidgetMVC.Widget(model, view, controller, rootNode, this.widget, true);
        //widget.parent = this.widget;
        //return widget;
    } 

/widgetmvc.js

export class Widget {
    model = null;
    view = null;
    controller = null;
    parent = null;

    constructor(m = null, v = null, c = null, rootNode = RootNode, parent = null, assetIo = AssetIo) {
        this.parent = parent;
        this.model = new m();
        this.view = new v(rootNode, assetIo);
        this.controller = new c();
        this.model.setController(this.controller);
        this.view.setController(this.controller);
        this.controller.setModel(this.model);
        this.controller.setView(this.view);
        this.model.widget = this;
        this.view.widget = this;
        this.controller.widget = this;
        this.model.awake();
        this.view.awake();
        this.controller.awake();
    }
lucidium4 commented 1 month ago

The bug seems to occur when trying to instantiate a class from an es module

lahma commented 1 month ago

This needs a proper repro without need to learn a game engine and a system on top of that. Can not fix otherwise.

lucidium4 commented 1 month ago

I found a workaround on my End