| 21 | import org.mozilla.javascript.xml.XMLObject; |
| 22 | |
| 23 | class XML extends XMLObjectImpl { |
| 24 | static final long serialVersionUID = -630969919086449092L; |
| 25 | |
| 26 | private XmlNode node; |
| 27 | |
| 28 | private static final ClassDescriptor DESCRIPTOR; |
| 29 | static final SymbolKey LIB_KEY = new SymbolKey("__xml_lib__", REGULAR); |
| 30 | |
| 31 | static { |
| 32 | DESCRIPTOR = |
| 33 | XMLObjectImpl.populatePrototypeDescriptor( |
| 34 | new ClassDescriptor.Builder( |
| 35 | "XML", 1, XML::js_constructorCall, XML::js_constructor)) |
| 36 | .withMethod(CTOR, SymbolKey.HAS_INSTANCE, 1, XML::js_hasInstance) |
| 37 | .build(); |
| 38 | } |
| 39 | |
| 40 | public static void init( |
| 41 | Context cx, VarScope scope, XMLObjectImpl proto, boolean sealed, XMLLibImpl lib) { |
| 42 | DESCRIPTOR.buildConstructor( |
| 43 | cx, |
| 44 | scope, |
| 45 | proto, |
| 46 | sealed, |
| 47 | (ctx, ctor) -> { |
| 48 | ctor.put(LIB_KEY, ctor, lib); |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | private static Object js_constructor( |
| 53 | Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) { |
| 54 | XMLLibImpl lib = (XMLLibImpl) f.get(LIB_KEY, f); |
| 55 | if (args.length == 0 || args[0] == null || args[0] == Undefined.instance) { |
| 56 | args = new Object[] {""}; |
| 57 | } |
| 58 | // ECMA 13.4.2 does not appear to specify what to do if multiple arguments are sent. |
| 59 | XML toXml = lib.ecmaToXml(args[0]); |
| 60 | return toXml.copy(); |
| 61 | } |
| 62 | |
| 63 | private static Object js_constructorCall( |
| 64 | Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) { |
| 65 | XMLLibImpl lib = (XMLLibImpl) f.get(LIB_KEY, f); |
| 66 | if (args.length == 0 || args[0] == null || args[0] == Undefined.instance) { |
| 67 | args = new Object[] {""}; |
| 68 | } |
| 69 | // ECMA 13.4.2 does not appear to specify what to do if multiple arguments are sent. |
| 70 | XML toXml = lib.ecmaToXml(args[0]); |
| 71 | return toXml; |
| 72 | } |
| 73 | |
| 74 | static Object js_hasInstance( |
| 75 | Context cx, JSFunction f, Object nt, VarScope s, Object thisObj, Object[] args) { |
| 76 | if (args.length == 0 || !(args[0] instanceof Scriptable)) { |
| 77 | Object v = args.length == 0 ? null : args[0]; |
| 78 | throw ScriptRuntime.typeErrorById("msg.arg.not.object", ScriptRuntime.typeof(v)); |
| 79 | } |
| 80 |
no test coverage detected