How to link javascript UIWebView exception source code to source file?

Short question: what does the exception "sourceID" refer to and how can I associate it with the corresponding source line / file?

Longer:

I run Javascript code in my own iPhone application through [UIWebView stringByEvaluatingJavaScriptFromString:]. To help develop and then verify the code provided by the user, I use the following function to safely run any code:

// Inside @implementation MyJS
- (NSString *)runJS:(NSString *)js {
    // Do some escaping on 'js' to make it look like a string literal.
    js = escape(js);
    NSString *result =
        [webView stringByEvaluatingJavaScriptFromString:
            [NSString stringWithFormat:@"try { JSON.stringify(eval(\"%@\")); } except (e) { JSON.stringify(e); }", js]
        ];
    return result;
}

If everything goes well, it [MyJS runJS:js]works fine and returns a JSON string containing the result of evaluating the last statement in the 'js' code.

Now, if bad things happen during the evaluation, I get a JSONified exception object. For example, in the case of a syntax error in the "js" code, I get something like this:

{ "message": "Parse error", "line": 1, "sourceId": 26121296}

...

, runJS:, , ( , javascript). sourceId , , . ( ), , . ?

: - javascript, UIWebView, , ? - Safari , .

+3
1

:

js , , , sourceId - , runJS: , sourceId .

(, - !)

+2

Source: https://habr.com/ru/post/1728006/


All Articles