IOS remote code delivery, what are the design ideas?

Most people use JSPatch, right?

It’s never good to use someone else’s. Lao Wang built a wheel. Let me describe it.

Wang Patch, it’s pretty advanced. JavaScript code, packaged with WebPack. Also with reference to JSPatch,

How to design this Patch?

In principle


What is Patch mainly for? In the process of running the App of a large company, the business line is very complicated and some problems may occur.

At this point, it’s better to patch

A file can be sent remotely, and the App can implement the code patch by loading this file.

This way, you can remotely execute this code while you’re developing your app,

OC is ok, because it is a dynamic language and has Runtime, so we can make this patch.

The OC has its own message forwarding process. The Runtime has objc_msgSend and _objc_msgForward. The Runtime has these two methods, and these two function features make all function calls go through these two methods.

In this way, I can do some patch things.

On the other hand, when the patch is implanted in the App, the language it uses can be eval.

Eval, to determine that a code statement can be executed

So there’s a context in the app that can execute the language for patch.


The iOS patch in this article directly uses the JavaScriptCore JSContext provided by JavaScriptCore. It allows you to have an interface between the JavaScript language and Objective-C. And then you can call JavaScript and Objective-C to each other. (JavaScriptCore, implemented by Apple, of course)

JSPatch uses FFI, as does the Kuraowang Patch.

FFI, Foreign Function Interface, is an Interface that exposes one language to other languages. The Java JNI standard is similar to FFI. FFI first complies with the Coding Convention, which defines protocols and rules for complying with certain calls. Why FFI?

An 🌰 :

When we make a function call, we first open a stack frame. What parameters does the stack frame pass? What is the type of the parameter? The number of arguments, what is that? What are some of the actions that are performed, including in this function? FFI defines the protocols and rules for some of the calls to be followed.


The custom FFI of The Patch library of Lao Wang is to connect the Patch language to the execution environment of Objective-C. The commands that need to be passed in, specify this thing.

In the process of executing the Patch, you can follow this command to send the corresponding message, so that the corresponding context can execute the code requiring the Patch.

That’s it for FFI. Enable precompilation.

In the process of using JSPatch, Lao Wang found that he was not comfortable with it. Why was he not comfortable with it? According to Wang, JSPatch harddrives JavaScript into the equivalent of Objective-C style patches. A lot of language processing has been done, including the source function processing Bang said. Includes C language functions, similar to Ruby’s Method Mission. As for the rest, Wang thinks JSPatch is on the back end.

Fidelity of JavaScript

To strengthen Patch, one of the smoothness of the language itself is to write JavaScript, which is used to write

This leads to the precompilation process:

The JavaScriptContext registration interface is abstracted into three layers,

Define,

Define, send a message to Objective-C. You don’t need to return a parameter, you can use define. If you need to define, you must define yourself. I can just abstract out this layer.

evaluate

What tasks are used to perform when a return value is required

Callback, multilayer encapsulation deep, more troublesome

Wang feels that there may be some issues with apple’s packaged JavaScriptContext. For example, some function objects passed by the developer encapsulate two layers. Then you might not get the object. At this point, the Callback method is needed to retrieve the front-end function content in that execution environment. Retrieves a function value from the current JavaScriptContext using a javascriptCore-related callback.

Then there are the instruction types

What is this patch for? What are the instructions for what to use?

  • A Patch is a hook that performs some Method function,
  • Some property values will be changed.
  • There will be some block changes.
  • Access the superclass super
  • You can add a new method, method_create

With these instructions, we can implement some of the functions that we want to patch to

There is also keyword conversion: this is done through preprocessing. We need to finish processing some keywords.

The self keyword in Objective-C is usually used to represent a pointer to the current object. We also need to change the super keyword to oc_super, since super is also a keyword in ECMAScript 6 and needs to be avoided. Original, which is unique to Patch. Call the previous function, that is, call the function before the patch.

Some optimizations, for example 🌰 : JS write higher-order functions

Do a smoothing of JavaScript higher-order functions that would otherwise be laborious to write.

a.request( 

function(a:id,b:Int):double{},
callback:(string,string) => int,
(num1:string, num2:double) => { returnnum1 + num2; });Copy the code

Request method, which takes three arguments. Three parameters of type function. The first function, the function, takes two arguments. The second argument, callback, is taken in the context of the function call. The third parameter is the arrow function used in ECMAScript 6. Better yet, have some support for this. These operations are preprocessed and compiled into the OC Block instruction.

I can almost finish my work normally.

JSPatch is a good reference,

Like JSPatch, everyone registers a bunch of JavaScriptContext functions that they want to patch. Add what you want to use. It usually borrows entirely from Bang’s ideas.

The best? Some of the ideas behind JSPatch need to be abstracted.

The shortcoming of Lao Wang Patch is that it does not use JSExport protocol, JSVirtualMachine

Making official repo link: https://github.com/wangyunico/iOSPatchBackend

My private repo, https://github.com/BoxDengJZ/lao_wang_patch