execution of StringExtract and MBStringExtract implemented

This commit is contained in:
honfika@gmail.com
2015-11-03 11:36:32 +01:00
parent e7bf1b0319
commit 58596ca5e5
4 changed files with 82 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.model;
import com.jpexs.decompiler.flash.SourceGeneratorLocalData;
import com.jpexs.decompiler.flash.action.swf4.ActionMBStringExtract;
import com.jpexs.decompiler.flash.ecma.EcmaScript;
import com.jpexs.decompiler.flash.helpers.GraphTextWriter;
import com.jpexs.decompiler.graph.CompilationException;
import com.jpexs.decompiler.graph.GraphSourceItem;
@@ -72,6 +73,35 @@ public class MBStringExtractActionItem extends ActionItem {
return ret;
}
@Override
public Object getResult() {
return getResult(count.getResult(), index.getResult(), value.getResult());
}
public static String getResult(Object count, Object index, Object value) {
String str = EcmaScript.toString(value);
int idx = (int) (double) EcmaScript.toNumber(index);
idx--; // index seems to be 1 based
int cnt = (int) (double) EcmaScript.toNumber(count);
/*if (idx < 0) {
idx = str.length() + idx;
}*/
if (idx < 0) {
idx = 0;
} else if (idx > str.length()) {
return "";
}
if (cnt < 0) {
cnt = str.length();
}
int endIdx = Math.min(str.length(), idx + cnt);
return str.substring(idx, endIdx);
}
@Override
public List<GraphSourceItem> toSource(SourceGeneratorLocalData localData, SourceGenerator generator) throws CompilationException {
return toSourceMerge(localData, generator, value, index, count, new ActionMBStringExtract());

View File

@@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.model;
import com.jpexs.decompiler.flash.SourceGeneratorLocalData;
import com.jpexs.decompiler.flash.action.swf4.ActionStringExtract;
import com.jpexs.decompiler.flash.ecma.EcmaScript;
import com.jpexs.decompiler.flash.helpers.GraphTextWriter;
import com.jpexs.decompiler.graph.CompilationException;
import com.jpexs.decompiler.graph.GraphSourceItem;
@@ -61,6 +62,35 @@ public class StringExtractActionItem extends ActionItem {
return ret;
}
@Override
public Object getResult() {
return getResult(count.getResult(), index.getResult(), value.getResult());
}
public static String getResult(Object count, Object index, Object value) {
String str = EcmaScript.toString(value);
int idx = (int) (double) EcmaScript.toNumber(index);
idx--; // index seems to be 1 based
int cnt = (int) (double) EcmaScript.toNumber(count);
/*if (idx < 0) {
idx = str.length() + idx;
}*/
if (idx < 0) {
idx = 0;
} else if (idx > str.length()) {
return "";
}
if (cnt < 0) {
cnt = str.length();
}
int endIdx = Math.min(str.length(), idx + cnt);
return str.substring(idx, endIdx);
}
@Override
public List<GraphSourceItem> toSource(SourceGeneratorLocalData localData, SourceGenerator generator) throws CompilationException {
return toSourceMerge(localData, generator, value, index, count, new ActionStringExtract());

View File

@@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.swf4;
import com.jpexs.decompiler.flash.BaseLocalData;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.action.LocalDataArea;
import com.jpexs.decompiler.flash.action.model.MBStringExtractActionItem;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
@@ -35,6 +36,16 @@ public class ActionMBStringExtract extends Action {
return "MBStringExtract";
}
@Override
public boolean execute(LocalDataArea lda) {
if (lda.stack.size() < 3) {
return false;
}
lda.stack.push(MBStringExtractActionItem.getResult(lda.pop(), lda.pop(), lda.pop()));
return true;
}
@Override
public void translate(TranslateStack stack, List<GraphTargetItem> output, HashMap<Integer, String> regNames, HashMap<String, GraphTargetItem> variables, HashMap<String, GraphTargetItem> functions, int staticOperation, String path) {
GraphTargetItem count = stack.pop();

View File

@@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.swf4;
import com.jpexs.decompiler.flash.BaseLocalData;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.action.LocalDataArea;
import com.jpexs.decompiler.flash.action.model.StringExtractActionItem;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
@@ -35,6 +36,16 @@ public class ActionStringExtract extends Action {
return "StringExtract";
}
@Override
public boolean execute(LocalDataArea lda) {
if (lda.stack.size() < 3) {
return false;
}
lda.stack.push(StringExtractActionItem.getResult(lda.pop(), lda.pop(), lda.pop()));
return true;
}
@Override
public void translate(TranslateStack stack, List<GraphTargetItem> output, HashMap<Integer, String> regNames, HashMap<String, GraphTargetItem> variables, HashMap<String, GraphTargetItem> functions, int staticOperation, String path) {
GraphTargetItem count = stack.pop();