MakeFullWrapper creates a JavaScript object which has wrappers for the exported methods of i, and, where i is a (pointer to a) struct value, wrapped getters and setters (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) for the non-embedded expor
(i any)
| 161 | // and getters are themselves wrapped when accessed, but an important point to |
| 162 | // note is that a new wrapped value is created on each access. |
| 163 | func MakeFullWrapper(i any) *Object { |
| 164 | internalObj := InternalObject(i) |
| 165 | constructor := internalObj.Get("constructor") |
| 166 | |
| 167 | wrapperObj := Global.Get("Object").New() |
| 168 | |
| 169 | defineProperty := func(key string, descriptor M) { |
| 170 | Global.Get("Object").Call("defineProperty", wrapperObj, key, descriptor) |
| 171 | } |
| 172 | |
| 173 | defineProperty("__internal_object__", M{ |
| 174 | "value": internalObj, |
| 175 | }) |
| 176 | |
| 177 | { |
| 178 | // Calculate a sensible type string. |
| 179 | |
| 180 | // We don't want to import any packages in this package, |
| 181 | // so we do some string operations by hand. |
| 182 | |
| 183 | typ := constructor.Get("string").String() |
| 184 | pkg := constructor.Get("pkg").String() |
| 185 | |
| 186 | ptr := "" |
| 187 | if typ[0] == '*' { |
| 188 | ptr = "*" |
| 189 | } |
| 190 | |
| 191 | for i := 0; i < len(typ); i++ { |
| 192 | if typ[i] == '.' { |
| 193 | typ = typ[i+1:] |
| 194 | break |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | pkgTyp := pkg + "." + ptr + typ |
| 199 | defineProperty("$type", M{ |
| 200 | "value": pkgTyp, |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | var fields *Object |
| 205 | methods := Global.Get("Array").New() |
| 206 | if ms := constructor.Get("methods"); ms != Undefined { |
| 207 | methods = methods.Call("concat", ms) |
| 208 | } |
| 209 | // If we are a pointer value then add fields from element, |
| 210 | // else the constructor itself will have them. |
| 211 | if e := constructor.Get("elem"); e != Undefined { |
| 212 | fields = e.Get("fields") |
| 213 | methods = methods.Call("concat", e.Get("methods")) |
| 214 | } else { |
| 215 | fields = constructor.Get("fields") |
| 216 | } |
| 217 | for i := 0; i < methods.Length(); i++ { |
| 218 | m := methods.Index(i) |
| 219 | if m.Get("pkg").String() != "" { // not exported |
| 220 | continue |
searching dependent graphs…