Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jscore中类的使用问题 #1

Closed
xioxin opened this issue Mar 15, 2020 · 1 comment
Closed

jscore中类的使用问题 #1

xioxin opened this issue Mar 15, 2020 · 1 comment

Comments

@xioxin
Copy link

xioxin commented Mar 15, 2020

我在dart里创建的类,js里初始化后返回到dart再取值发现取到的都是最后一个

js执行的: [new Text('测试1'), new Text('测试2'), new Text('测试3')];
到dart中取到的内容是 (测试3, 测试3, 测试3)
全部变成最后一个了

大佬帮忙看看是不是用的方法不对.

完整代码如下:

class TestBug extends StatelessWidget {

  static Pointer jsClassInitialize(
      Pointer ctx,
      Pointer constructor,
      int argumentCount,
      Pointer<Pointer> arguments,
      Pointer<Pointer> exception) {
    String text;
    final context = JSContext(ctx);
    final that = JSValue(context, constructor).toObject();
    if (argumentCount >= 1) {
      final arg1 = JSValue(context, arguments[0]);
      if (arg1.isString) {
        text = arg1.string;
      }
    }
    that.setProperty(
        'text',
        JSValue.makeString(context, text),
        JSPropertyAttributes.kJSPropertyAttributeDontDelete);
    return constructor;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: RaisedButton(
            child: Text('TEST'),
            onPressed: () {
              final jsContext = JSContext.createInGroup();
              final classDef = JSClassDefinition(
                version: 0,
                attributes: JSClassAttributes.kJSClassAttributeNone,
                className: 'Text',
                callAsConstructor: Pointer.fromFunction(jsClassInitialize),
                staticFunctions: [],
              );
              var flutterJSClass = JSClass.create(classDef);
              var flutterJSObject = JSObject.make(jsContext, flutterJSClass);
              jsContext.globalObject.setProperty('Text', flutterJSObject.toValue(),
                  JSPropertyAttributes.kJSPropertyAttributeDontDelete);
              final data = jsContext.evaluate("""
              [new Text('测试1'), new Text('测试2'), new Text('测试3')]
            """);
              final list = jsValueToList(data).map((e) => e.toObject().getProperty('text').string);
              print(list);
             // I/flutter ( 8752): (测试3, 测试3, 测试3)
            },
          )),
    );
  }
}
@xioxin
Copy link
Author

xioxin commented Mar 17, 2020

经过尝试代码改成这样就可以恢复正常
final that = JSValue(context, constructor).toObject(); 替换成 final that = JSObject.make(context, jsClass);
这里相当于重新new了个类返回出去了.
jsClassInitialize方法会被多触发一遍.
不清楚是不是正确的用法,不过暂时解决问题了。

class TestBug extends StatelessWidget {
  static JSClassDefinition classDef = JSClassDefinition(
    version: 0,
    attributes: JSClassAttributes.kJSClassAttributeNone,
    className: 'Text',
    initialize: Pointer.fromFunction(jsClassInitialize),
    callAsConstructor: Pointer.fromFunction(jsClassConstructor),
    staticFunctions: [],
  );
  static JSClass jsClass = JSClass.create(classDef);

  static Pointer jsClassConstructor(
      Pointer ctx,
      Pointer constructor,
      int argumentCount,
      Pointer<Pointer> arguments,
      Pointer<Pointer> exception) {
    String text;
    final context = JSContext(ctx);
    final that = JSObject.make(context, jsClass);
    if (argumentCount >= 1) {
      final arg1 = JSValue(context, arguments[0]);
      if (arg1.isString) {
        text = arg1.string;
      }
    }
    that.setProperty('text', JSValue.makeString(context, text),
        JSPropertyAttributes.kJSPropertyAttributeDontDelete);
    return that.pointer;
  }

  static void jsClassInitialize(Pointer ctx, Pointer object) {
    print('jsClassInitialize');
//    final context = JSContext(ctx);
//    final obj = JSObject(context, object);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: RaisedButton(
        child: Text('TEST'),
        onPressed: () {
          final jsContext = JSContext.createInGroup();
          var flutterJSObject = JSObject.make(jsContext, jsClass);
          jsContext.globalObject.setProperty('Text', flutterJSObject.toValue(),
              JSPropertyAttributes.kJSPropertyAttributeDontDelete);
          final data = jsContext.evaluate("""
              [new Text('测试1'), new Text('测试2'), new Text('测试3')]
            """);
          final list = jsValueToList(data)
              .map((e) => e.toObject().getProperty('text').string);
          print(list);
        },
      )),
    );
  }
}

@xioxin xioxin closed this as completed Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant