c-smile / sciter-sdk

Sciter is an embeddable HTML/CSS/scripting engine
http://sciter.com
Other
2.11k stars 224 forks source link

Variable not found - ? when parseData #108

Closed woodgear closed 5 years ago

woodgear commented 5 years ago
<html>
    <head>
        <title>Test</title>
        <script type="text/tiscript">
        function getData(path) {
            var config = Stream.openFile(path);
            if(config)
            {
                stdout.println("access file is correct");
                var jsonData=null;
                try {
                    //the assign expression and parseData work correct
                    jsonData = parseData("{}");
                    stdout.println("config is accesable",config);
                    // so what happen now?
                    jsonData = parseData(config);
                    stdout.println("after parseData");

                } catch(e) {
                    stdout.println("parseData err ",e);
                }

                if(jsonData){
                    stdout.printf("JSON data is : %v\n", jsonData);
                } else {
                    stdout.println("No data");
                }

                config.close();
            }else {
                stdout.println("could not find file ",path);                
            }
        }
//        getData("C:\\Users\\developer\\Desktop\\test_sciter_file\\config.json")
        getData("config.json")

    </script>
    </head>
    <body>
        <button #t>TEST</button>
    </body>
</html>
INFO:TIS: access file is correct
INFO:TIS: config is accesable [object Stream]
INFO:TIS: parseData err  Error: Variable not found - ?
INFO:TIS:       at file://C:/Users/developer/Desktop/test_sciter_file/main.html
INFO:TIS:       at undefined
INFO:TIS: JSON data is : {}
c-smile commented 5 years ago

Seems like you have an error in your config.json as this works OK:

<html>
    <head>
        <title>Test</title>
        <script type="text/tiscript">
        function getData(path) {
            var config = Stream.openString("{foo:42}");
            if(config)
            {
                stdout.println("access file is correct");
                var jsonData=null;
                try {
                    //the assign expression and parseData work correct
                    jsonData = parseData("{}");
                    stdout.println("config is accesable",config);
                    // so what happen now?
                    jsonData = parseData(config);
                    stdout.println("after parseData");

                } catch(e) {
                    stdout.println("parseData err ",e);
                }

                if(jsonData){
                    stdout.printf("JSON data is : %v\n", jsonData);
                } else {
                    stdout.println("No data");
                }

                config.close();
            }else {
                stdout.println("could not find file ",path);                
            }
        }
//        getData("C:\\Users\\developer\\Desktop\\test_sciter_file\\config.json")
        getData("config.json")

    </script>
    </head>
    <body>
        <button #t>TEST</button>
    </body>
</html>
woodgear commented 5 years ago

same result even i change config.json

PS C:\Users\developer\Desktop\test_sciter_file> cat .\config.json
{ name:"John" }
woodgear commented 5 years ago

is there someway to make a stream into a string? the toString method just return the file name.

c-smile commented 5 years ago

File test.htm:

<html>
    <head>
        <title>Test</title>
        <script type="text/tiscript">
        function getData(path) {
            var config = Stream.openFile(path,"ru");
            if(config)
            {
                stdout.println("access file is correct");
                var jsonData=null;
                try {
                    //the assign expression and parseData work correct
                    jsonData = parseData("{}");
                    stdout.println("config is accesable",config);
                    // so what happen now?
                    jsonData = parseData(config);
                    stdout.println("after parseData");

                } catch(e) {
                    stdout.println("parseData err ",e);
                }

                if(jsonData){
                    stdout.printf("JSON data is : %v\n", jsonData);
                } else {
                    stdout.println("No data");
                }

                config.close();
            }else {
                stdout.println("could not find file ",path);                
            }
        }
//        getData("C:\\Users\\developer\\Desktop\\test_sciter_file\\config.json")
        getData(__FOLDER__ + "config.json");

    </script>
    </head>
    <body>
        <button #t>TEST</button>
    </body>
</html>

File config.json in the same folder:

{ name:"John" }

looks ok here. Check if your file is UTF-8 encoded.

As of reading file stream:

function streamToString(stream) {
  var out = "";
  while(true) {
      var line =  stream.readln();
      if( line === undefined ) break;
      out += line + "\n";
  } 
  return out;
}
woodgear commented 5 years ago

thanks a lot. the cause of this question is that config.json is been encoded as 'utf8-with-bom'.