JSObject- JavaScript 数组是否作为 Java 数组传递?

发布于 2024-12-11 22:45:28 字数 835 浏览 0 评论 0原文

我正在尝试将复杂的 Java 小程序转换为 JavaScript。该小程序通过 TCP 套接字进行通信,发挥了各种作用,我需要在 JavaScript 中对其进行模拟。 TCP 魔法本身相当复杂,所以我宁愿稍后再做,先让小程序的图形部分工作。 TCP 套接字上的通信告诉小程序要绘制什么,因此根据套接字上的数据,小程序将读取/写入不同的值。

我当前的攻击计划是:

  • 将请求标头作为参数(工作)以及来自套接字的所需数据传递给 JavaScript,具体取决于请求类型
  • 解析在 JS 中发送的标头参数/数据并相应地创建对象
  • 返回值以使用数组调用 Java 函数要写入 TCP 套接字的字节数

我陷入了最后一部分。如果我返回一个字节数组,如何将其转换为 byte[] 或类似的?传回的值是一个正确的 Java 数组还是某种对象哈希?

Java:

void callJavaScript(Applet app) {
    String[] params = {"blah", "cool"};

    JSObject win = JSObject.getWindow(app);
    Object ret = win.call("someFunction", params);

    // what is ret?
}

JavaScript:

function someFunction (blah, cool) {
    return [5, 7, 12, 2];
}

注意:

复杂的Java applet 是多线程的,因此仅调用applet 中的某些函数就很重要。

I'm trying to convert a complex Java applet to JavaScript. The applet does all sorts of magic with communicating on a TCP socket, which I will need to emulate in JavaScript. The TCP magic is itself quite complicated, so I'd rather do that later and get the graphical part of the applet working first. The communication over the TCP socket tells the applet what to draw, so depending on the data on the socket, the applet will read/write different values.

My current plan of attack is:

  • Pass request header to JavaScript as parameters (working) as well as needed data from socket depending on the request type
  • Parse header params/data sent in JS and create objects accordingly
  • Return value to calling Java function with an array of bytes to be written to the TCP socket

I'm stuck on the last part. If I return an array of bytes, how can I convert that to a byte[] or similar? Is the value passed back a proper Java array or is it some kind of object hash?

Java:

void callJavaScript(Applet app) {
    String[] params = {"blah", "cool"};

    JSObject win = JSObject.getWindow(app);
    Object ret = win.call("someFunction", params);

    // what is ret?
}

JavaScript:

function someFunction (blah, cool) {
    return [5, 7, 12, 2];
}

Note:

The complicated Java applet is multi-threaded, so just calling some function in the applet is non-trivial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2024-12-18 22:45:28

刚刚写了一个示例来检查 =)

Test.java:

import java.applet.Applet;
import java.awt.*;
import netscape.javascript.JSObject;

public class Test extends Applet {
  public void init() {
    JSObject window = JSObject.getWindow(this); // this=applet
    String[] params = {"blah", "cool"};
    Object ret = window.call("testfunc", params);
    if (ret instanceof JSObject){
      JSObject jsret = (JSObject)ret;
      Object len = jsret.getMember("length");
      String val = "";
      if (len instanceof Number){
        val = "array: ";
        int n = ((Number)len).intValue();
        for (int i = 0; i < n; ++i){
            val += jsret.getSlot(i).toString() + " ";
        }
      } else{
        val = "no array: " + ret.toString();
      }
      String[] params2 = {val};
      window.call("alert", params2);
    } else{
      String[] params2 = {ret.toString()};
      window.call("alert", params2);
    }
  }
}

Test.html:

<html>
  <head>
    <title> Test </title>
      <script language="JavaScript">
        function testfunc(a, b){
          return [1, 2];
        }
      </script>
    </head>
    <body>
      <applet code="Test.class" archive="." width="500" height="200"><param name="codebase_lookup" value="false"></applet>
    </body>
</html>

转换显示在 docstore.mik.ua/orelly/web/jscript/ch19_06.html 看起来有效(图 19.5 和 19.6)。您可以在我的代码中看到我检查了 len instanceof Number 而不是 len instanceof Double 因为在我的 IE 中它返回了一个 Integer

Did just write a sample to check =)

Test.java:

import java.applet.Applet;
import java.awt.*;
import netscape.javascript.JSObject;

public class Test extends Applet {
  public void init() {
    JSObject window = JSObject.getWindow(this); // this=applet
    String[] params = {"blah", "cool"};
    Object ret = window.call("testfunc", params);
    if (ret instanceof JSObject){
      JSObject jsret = (JSObject)ret;
      Object len = jsret.getMember("length");
      String val = "";
      if (len instanceof Number){
        val = "array: ";
        int n = ((Number)len).intValue();
        for (int i = 0; i < n; ++i){
            val += jsret.getSlot(i).toString() + " ";
        }
      } else{
        val = "no array: " + ret.toString();
      }
      String[] params2 = {val};
      window.call("alert", params2);
    } else{
      String[] params2 = {ret.toString()};
      window.call("alert", params2);
    }
  }
}

Test.html:

<html>
  <head>
    <title> Test </title>
      <script language="JavaScript">
        function testfunc(a, b){
          return [1, 2];
        }
      </script>
    </head>
    <body>
      <applet code="Test.class" archive="." width="500" height="200"><param name="codebase_lookup" value="false"></applet>
    </body>
</html>

The conversions show in docstore.mik.ua/orelly/web/jscript/ch19_06.html seen to work(Figures 19.5 and 19.6). You can see in my code I checked len instanceof Number and not len instanceof Double because in my IE it returned an Integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文