Implementing ||= (object,object) operator for older browsers

48 Views Asked by At

Android WebView have trouble with emscripten generated code. When trying to load page, it complains = is unexpected in ||= string. ||= is logical OR operator with assignment. I am locking for documentation in case, where both operands are objects. I think it simply copy each property of right-side onto left-side with overlap, so if right object had property a = 1 and left object had a = 2, result object will have a = 1. Have I right?

Edit 1:

I read comments, thanks, but I have trouble with add support for new operator. Not all languages allowing to define custom operators, like nim.

My implementation looks like:

object.prototype["||="] = function (b) {
    
      if (! this) {
        this.prototype = b;
      }
    }

It does not generate errors on browsers, which supports this operator, but do not take effect on Android WebView. Some way to solve this?

Edit 2:

I wrote this Java code:

 try {
        if (extension != null) {

            if (extension.equals("js")) {
                byte bytes[] = new byte[999999];
              
                input.read(bytes);
                String content = new String(bytes, StandardCharsets.UTF_8);
               

                content.replaceAll("([a-zA-Z0-9\\.\\_]+) \\|\\|\\= (\\{((?!\\}\\;)|\n)+\\}\\;)",
                        "replaceEmpty(\\1, \\2)");
             
                input = new ByteArrayInputStream(content.getBytes());
                
            }
           
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
     
        }
    }
    catch (Exception a) {

    }
    return new WebResourceResponse(type, "UTF-8", input);

But I probably makes mistake with regexp. Could I use regexp with substitutions (parameters/string from input to output string)?

I am trying to replace string like:

Object1 ||= {
      prop1: "value",
      prop2: value2,
      subobject: {
      },
      array: [],
      ...
    };

So I cannot do this, but I solved my issue. Code

  InputStream input = activity.getAssets().open("Game" + url);
        //FileInputStream input = new FileInputStream(url);
                String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);

       try {
        if (extension != null) {
            if (extension.equals("js")) {
                byte[] bytes = new byte[999999];
                

                int length = input.read(bytes);

                bytes[length] ='\0';

                byte[] bytes2 = new byte[length];
                System.arraycopy(bytes,0,  bytes2, 0, length);
                String content = new String(bytes2, StandardCharsets.UTF_8);;
               

                content = content.replaceAll("([a-zA-Z0-9\\.\\_]+?) \\|\\|\\=", "if (! $1 ) $1  =");

  

                
                
                input = new ByteArrayInputStream(content.getBytes());
                
            }
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
            }
    }
    catch (Exception a) {

    }

    return new WebResourceResponse(type, "UTF-8", input);

You can read regular expression I've used.

1

There are 1 best solutions below

0
nintyfan On
class MainWebClient extends WebViewClient {
....

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 

try {
         // In case, when we use domain name to detect if resource is local
         url = url.substring(prefix.length() - 1);
         // Game could be directory in assets, when we kept our webpagee
         InputStream input = activity.getAssets().open("Game" + url);
            
        String type = null;
        String extension = MimeTypeMap.getFileExtensionFromUrl(url);
        if (extension != null) {

            if (extension.equals("js")) {
                byte bytes[] = new byte[999999];
              
                input.read(bytes);
                String content = new String(bytes, StandardCharsets.UTF_8);
               

                content.replaceAll("([a-zA-Z0-9\\.\\_]+) \\|\\|\\= (\\{((?!\\}\\;)|\n)+\\}\\;)",
                        "replaceEmpty(\\1, \\2)");
             
                input = new ByteArrayInputStream(content.getBytes());
                
            }
           
            type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
     
        }
    }
    catch (Exception a) {

    }
    return new WebResourceResponse(type, "UTF-8", input);
}