microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.74k stars 148 forks source link

When passing global reference types to C#, the address is lost. #539

Closed Rison-Hub closed 1 year ago

Rison-Hub commented 1 year ago
  1. I defined a global variable to store the reference image. JS code:
    var AllMap = { Image: null};
    ZPV_Read_Image(AllMap,AllPaths.replace("Script", "AUTO Test System/Write/Image_Correction/QCP41/QCP41/Image/QCP41") + "Map.tif");
    function ZPV_Read_Image(objImage, FileName) {
    var FunctionName; 
    FunctionName = "ZPV_Read_Image";
    var sPar = [objImage, FileName]; 
    var oResult; 
    oResult = zpv.RunHalconCode(FunctionName, sPar); 
    objImage.Image = oResult[0];
    zpv.TestImage(objImage.Image);
    }

    The value address of the image obtained from oResult[0] is 0x0000029e400310.

C# Code

public object RunHalconCode(string FunctionName, ScriptObject sPar)
        {
            if (Mod_Variable.V_RegVideo == false)
            {
                var frm_about = new Fm_about();
                frm_about.ShowDialog(frm);
            }
            int length = sPar.PropertyIndices.Count(); 
            System.Object[] objectArray = new System.Object[length];
            for (int i = 0; i < length; i++)
            {
                objectArray[i] = sPar.GetProperty(i); 
            }
            var Rxobj = ZP_Halcon_HdevEngine2.RunHalconScript(frm, PageIndex, EditMain_frm, FunctionName, objectArray);
            object[] obj = (object[])Rxobj;
            return obj;
        }
public void TestImage(object add) 
        { 
            HObject hObject = new HObject();
            hObject = (HObject)add;
        }

add has an address of ‘0x000000000000000’ The address of add should be 0x0000029e400310 in this context.

But when I modify the JavaScript code as follows, everything works correctly. JS Code:

var AllMap = { Image: null};
ZPV_Read_Image(AllMap,AllPaths.replace("Script", "AUTO Test System/Write/Image_Correction/QCP41/QCP41/Image/QCP41") + "Map.tif");
function ZPV_Read_Image(AllMap, FileName) {
    var FunctionName; 
    FunctionName = "ZPV_Read_Image";
    var sPar = [AllMap , FileName]; 
    var oResult; 
    oResult = zpv.RunHalconCode(FunctionName, sPar); 
    AllMap.Image = oResult[0];
    zpv.TestImage(AllMap.Image);
}

When I pass global parameters into a method, shouldn't the method receive parameters that point to the reference addresses of what I passed in?