bind method

JsValue bind(
  1. dynamic object, {
  2. ClassInfo? classInfo,
  3. JsValue? classFunc,
})
override

Establish a binding relationship between dart and js object

Implementation

JsValue bind(dynamic object, {
  ClassInfo? classInfo,
  JsValue? classFunc,
}) {
  if (object == null)
    throw Exception("Object is null");
  Pointer classPtr;
  if (classFunc == null) {
    if (classInfo == null) {
      classPtr = _classList[0].ptr;
    } else {
      if (_classIndex.containsKey(classInfo.type)) {
        classPtr = _classIndex[classInfo.type]!.ptr;
      } else {
        addClass(classInfo);
        classPtr = _classList.last.ptr;
      }
    }
  } else {
    classPtr = (classFunc as IOJsValue)._ptr;
  }

  _arguments[0].type = ARG_TYPE_MANAGED_VALUE;
  _arguments[0].ptrValue = classPtr;
  return _action(JS_ACTION_BIND, 1,
    block: (results, len) {
      if (len == 1 && results[0].type == ARG_TYPE_RAW_POINTER) {
        Pointer rawPtr = results[0].ptrValue;
        _instances[rawPtr] = object;
        var ptr = binder.retainValue(_context, rawPtr);
        return IOJsValue._js(this, ptr.ref.ptrValue);
      } else {
        throw Exception("Wrong result");
      }
    },
  );
}