Skip to content

Commit

Permalink
[hapjs-platform#629]沙箱fix:快应用传递Undefined数据无法正常序列化导致的崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-chai authored and jianghai33 committed Dec 12, 2023
1 parent 762deb6 commit e9a87ba
Showing 1 changed file with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

import android.util.ArrayMap;
import android.util.Log;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Object;
import com.eclipsesource.v8.V8Value;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.FieldSerializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
Expand All @@ -18,6 +23,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hapjs.bridge.InstanceManager;
import org.hapjs.bridge.Response;
import org.hapjs.render.jsruntime.JsThread;
Expand Down Expand Up @@ -75,16 +81,29 @@ private void ensureRegister() {
kryo.register(org.hapjs.common.json.JSONObject.class);
kryo.register(org.hapjs.common.json.JSONArray.class);

V8Value undefined = V8.getUndefined();
kryo.register(undefined.getClass(), new FieldSerializer(kryo, undefined.getClass()) {
@Override
public void write(Kryo kryo, Output output, Object object) {
// 不序列化 Undefined 数据
}

@Override
public Object read(Kryo kryo, Input input, Class type) {
return V8.getUndefined();
}
});

kryo.register(StackTraceElement.class, new FieldSerializer(kryo, StackTraceElement.class) {
public void write (Kryo kryo, Output output, Object object) {
public void write(Kryo kryo, Output output, Object object) {
StackTraceElement stack = (StackTraceElement) object;
output.writeString(stack.getClassName());
output.writeString(stack.getMethodName());
output.writeString(stack.getFileName());
output.writeInt(stack.getLineNumber());
}

public Object read (Kryo kryo, Input input, Class type) {
public Object read(Kryo kryo, Input input, Class type) {
String className = input.readString();
String methodName = input.readString();
String fileName = input.readString();
Expand All @@ -95,28 +114,28 @@ public Object read (Kryo kryo, Input input, Class type) {

Class clazz = ByteBuffer.wrap(new byte[1]).getClass();
kryo.register(clazz, new FieldSerializer(kryo, clazz) {
public void write (Kryo kryo, Output output, Object object) {
public void write(Kryo kryo, Output output, Object object) {
byte[] bytes = ((ByteBuffer) object).array();
output.writeInt(bytes.length);
output.write(bytes);
}

public Object read (Kryo kryo, Input input, Class type) {
public Object read(Kryo kryo, Input input, Class type) {
int len = input.readInt();
byte[] bytes = input.readBytes(len);
return ByteBuffer.wrap(bytes);
}
});

kryo.register(TypedArrayProxy.class, new FieldSerializer(kryo, TypedArrayProxy.class) {
public void write (Kryo kryo, Output output, Object object) {
public void write(Kryo kryo, Output output, Object object) {
byte[] bytes = ((TypedArrayProxy) object).getBytes();
output.writeInt(bytes.length);
output.write(bytes);
output.writeInt(((TypedArrayProxy) object).getType());
}

public Object read (Kryo kryo, Input input, Class type) {
public Object read(Kryo kryo, Input input, Class type) {
int len = input.readInt();
byte[] bytes = input.readBytes(len);
int dataType = input.readInt();
Expand Down

0 comments on commit e9a87ba

Please sign in to comment.