Refactoring

This commit is contained in:
Oleg Korshul
2022-05-24 19:52:50 +03:00
parent 459313292e
commit 42acdac865
5 changed files with 8 additions and 386 deletions

View File

@ -1,107 +0,0 @@
import sys
sys.path.append("../../../build_tools/scripts")
import base
import os
base.configure_common_apps()
# remove previous version
if base.is_dir("./deploy"):
base.delete_dir("./deploy")
base.create_dir("./deploy")
# fetch emsdk
command_prefix = "" if ("windows" == base.host_platform()) else "./"
if not base.is_dir("emsdk"):
base.cmd("git", ["clone", "https://github.com/emscripten-core/emsdk.git"])
os.chdir("emsdk")
base.cmd(command_prefix + "emsdk", ["install", "latest"])
base.cmd(command_prefix + "emsdk", ["activate", "latest"])
os.chdir("../")
# compile
compiler_flags = ["-o zlib.js",
"-O3",
"-fno-rtti",
"-s WASM=1",
"-s ALLOW_MEMORY_GROWTH=1",
"-s FILESYSTEM=0",
"-s ENVIRONMENT='web,worker'"]
#"-s LLD_REPORT_UNDEFINED"]
exported_functions = ["_malloc",
"_free",
"_Zlib_Malloc",
"_Zlib_Free",
"_Zlib_Create",
"_Zlib_Open",
"_Zlib_Close",
"_Zlib_AddFile",
"_Zlib_RemoveFile",
"_Zlib_GetPaths",
"_Zlib_GetFile",
"_Zlib_Save"]
libZlib_src_path = "../src/zlib-1.2.11"
input_zlib_sources = ["inflate.c", "zutil.c", "adler32.c", "crc32.c", "inftrees.c",
"inffast.c", "deflate.c", "trees.c"]
libMinizip_src_path = "../src/zlib-1.2.11/contrib/minizip"
input_minizip_sources = ["unzip.c", "ioapi.c", "zip.c", "ioapibuf.c"]
sources = []
for item in input_zlib_sources:
sources.append(libZlib_src_path + '/' + item)
for item in input_minizip_sources:
sources.append(libMinizip_src_path + '/' + item)
sources.append("../src/ZipBuffer.cpp")
sources.append("wasm/src/base.cpp")
compiler_flags.append("-I../src/zlib-1.2.11")
compiler_flags.append("-D__linux__")
# arguments
arguments = ""
for item in compiler_flags:
arguments += (item + " ")
arguments += "-s EXPORTED_FUNCTIONS=\"["
for item in exported_functions:
arguments += ("'" + item + "',")
arguments = arguments[:-1]
arguments += "]\" "
for item in sources:
arguments += (item + " ")
# command
windows_bat = []
if base.host_platform() == "windows":
windows_bat.append("call emsdk/emsdk_env.bat")
windows_bat.append("call emcc " + arguments)
else:
windows_bat.append("#!/bin/bash")
windows_bat.append("source ./emsdk/emsdk_env.sh")
windows_bat.append("emcc " + arguments)
base.run_as_bat(windows_bat)
# finalize
base.replaceInFile("./zlib.js", "__ATPOSTRUN__=[];", "__ATPOSTRUN__=[function(){self.onEngineInit();}];")
base.replaceInFile("./zlib.js", "function getBinaryPromise(){", "function getBinaryPromise2(){")
zlib_js_content = base.readFile("./zlib.js")
desktop_fetch_content = base.readFile("./../../Common/js/desktop_fetch.js")
string_utf8_content = base.readFile("./../../Common/js/string_utf8.js")
engine_base_js_content = base.readFile("./wasm/js/zlib_new.js")
engine_js_content = engine_base_js_content.replace("//desktop_fetch", desktop_fetch_content)
engine_js_content = engine_js_content.replace("//string_utf8", string_utf8_content)
engine_js_content = engine_js_content.replace("//module", zlib_js_content)
# write new version
base.writeFile("./deploy/zlib.js", engine_js_content)
base.copy_file("./zlib.wasm", "./deploy/zlib.wasm")
base.copy_file("./wasm/js/index.html", "./deploy/index.html")
base.copy_file("./wasm/js/code.js", "./deploy/code.js")
base.delete_file("zlib.js")
base.delete_file("zlib.wasm")

View File

@ -55,12 +55,12 @@ window.onload = function()
var archive = window.nativeZlibEngine.save();
window.nativeZlibEngine.close();
window.nativeZlibEngine.open(archive2);
var files3 = window.nativeZlibEngine.files;
for (var _path in files3)
window.nativeZlibEngine.open(archive);
var files2 = window.nativeZlibEngine.files;
for (var _path in files2)
{
window.nativeZlibEngine.getFile(_path);
window.writeFile(_path, files3[_path]);
window.writeFile(_path, files2[_path]);
}
window.nativeZlibEngine.close();
};
@ -74,5 +74,5 @@ window.writeFile = function(path, file)
{
if (!file) return;
var dst = document.getElementById("main");
dst.innerHTML += path + ' ' + file.length + '\n';
dst.innerHTML += ("<p>" + path + ' ' + file.length + "</p>");
};

View File

@ -296,9 +296,10 @@
};
window.nativeZlibEngine = new ZLib();
window.onEngineInit = function()
window.onZlibEngineInit = function()
{
window.nativeZlibEngine.isModuleInit = true;
window["ZLibModule_onLoad"] && window["ZLibModule_onLoad"]();
};
})(window, undefined);

View File

@ -1,272 +0,0 @@
(function(window, undefined){
//desktop_fetch
//string_utf8
//module
/**
* Class representing a zip archive creator/reader.
* @constructor
*/
function ZLib()
{
this.engine = 0; // указатель на нативный класс Zlib
this.files = {};
}
/**
* Check loaded wasm/asmjs module
*/
ZLib.prototype.isModuleInit = false;
/**
* Open archive from bytes
* @param {Uint8Array | ArrayBuffer} buf
* @returns {boolean} success or not
*/
ZLib.prototype.open = function(buf)
{
if (!this.isModuleInit)
return false;
if (this.engine)
this.close();
if (!buf)
return false;
var arrayBuffer = (undefined !== buf.byteLength) ? new Uint8Array(buf) : buf;
// TODO: открыли архив, и заполнили this.files
// объектами { path : null }
// копируем память в память webasm
var FileRawDataSize = arrayBuffer.length;
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
if (0 == FileRawData)
return false;
Module["HEAP8"].set(arrayBuffer, FileRawData);
// грузим данные
this.engine = Module["_Zlib_Open"](FileRawData, FileRawDataSize);
if (0 == this.engine)
{
Module["_Zlib_Free"](FileRawData);
return false;
}
// получаем пути в архиве
var pointer = Module["_Zlib_GetPaths"](this.engine);
if (0 == pointer)
{
Module["_Zlib_Close"](this.engine);
Module["_Zlib_Free"](FileRawData);
return false;
}
var lenArray = new Int32Array(Module["HEAP8"].buffer, pointer, 4);
var len = lenArray[0];
len -= 4;
var buffer = new Uint8Array(Module["HEAP8"].buffer, pointer + 4, len);
var index = 0;
while (index < len)
{
var lenRec = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
index += 4;
var _path = "".fromUtf8(buffer, index, lenRec);
this.files[_path] = null;
index += lenRec;
}
Module["_Zlib_Free"](FileRawData);
Module["_Zlib_Free"](pointer);
return true;
};
/**
* Create new archive
* @returns {boolean} success or not
*/
ZLib.prototype.create = function()
{
if (!this.isModuleInit)
return false;
if (this.engine)
this.close();
this.engine = Module["_Zlib_Create"]();
return !!this.engine;
};
/**
* Save archive from current files
* @returns {Uint8Array | null} zip-archive bytes, or null if error
*/
ZLib.prototype.save = function()
{
if (!this.isModuleInit || !this.engine)
return null;
var pointerZip = Module["_Zlib_Save"](this.engine);
if (0 == pointerZip)
return null;
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerZip, 4);
var len = _lenFile[0];
var zip = new Uint8Array(Module["HEAP8"].buffer, pointerZip + 4, len);
return zip;
};
/**
* Get uncomressed file from archive
* @param {string} path
* @returns {Uint8Array | null} bytes of uncompressed data, or null if error
*/
ZLib.prototype.getFile = function(path)
{
if (!this.isModuleInit || !this.engine)
return null;
// проверяем - есть ли файл вообще?
if (undefined === this.files[path])
return null;
// проверяем - может мы уже его разжимали?
if (null !== this.files[path])
{
if (this.files[path].l > 0)
{
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p, this.files[path].l);
}
else
{
var _lenFile = new Int32Array(Module["HEAP8"].buffer, this.files[path].p, 4);
var len = _lenFile[0];
return new Uint8Array(Module["HEAP8"].buffer, this.files[path].p + 4, len);
}
}
var tmp = path.toUtf8();
var pointer = Module["_Zlib_Malloc"](tmp.length);
if (0 == pointer)
return null;
Module["HEAP8"].set(tmp, pointer);
var pointerFile = Module["_Zlib_GetFile"](this.engine, pointer);
if (0 == pointerFile)
{
Module["_Zlib_Free"](pointer);
return null;
}
var _lenFile = new Int32Array(Module["HEAP8"].buffer, pointerFile, 4);
var len = _lenFile[0];
Module["_Zlib_Free"](pointer);
this.files[path] = { p : pointerFile, l : 0};
return new Uint8Array(Module["HEAP8"].buffer, pointerFile + 4, len);
};
/**
* Add uncomressed file to archive
* @param {string} path
* @param {Uint8Array} new file in archive
* @returns {boolean} success or not
*/
ZLib.prototype.addFile = function(path, data)
{
if (!this.isModuleInit || !this.engine)
return false;
if (!data)
return false;
// проверяем - может такой файл уже есть? тогда его надо сначала удалить?
if (undefined !== this.files[path])
this.removeFile(path);
var tmp = path.toUtf8();
var pointer = Module["_Zlib_Malloc"](tmp.length);
if (0 == pointer)
return false;
Module["HEAP8"].set(tmp, pointer);
var arrayBuffer = (undefined !== data.byteLength) ? new Uint8Array(data) : data;
var FileRawDataSize = arrayBuffer.length;
var FileRawData = Module["_Zlib_Malloc"](FileRawDataSize);
if (0 == FileRawData)
{
Module["_Zlib_Free"](pointer);
return false;
}
Module["HEAP8"].set(arrayBuffer, FileRawData);
Module["_Zlib_AddFile"](this.engine, pointer, FileRawData, FileRawDataSize);
this.files[path] = { p : FileRawData, l : FileRawDataSize};
Module["_Zlib_Free"](pointer);
return true;
};
/**
* Remove file from archive
* @param {string} path
* @returns {boolean} success or not
*/
ZLib.prototype.removeFile = function(path)
{
if (!this.isModuleInit || !this.engine)
return false;
// проверяем - может такого файла и нет?
if (undefined === this.files[path])
return false;
var tmp = path.toUtf8();
var pointer = Module["_Zlib_Malloc"](tmp.length);
if (0 == pointer)
return false;
Module["HEAP8"].set(tmp, pointer);
Module["_Zlib_RemoveFile"](this.engine, pointer);
if (this.files[path] && this.files[path].p)
{
Module["_Zlib_Free"](this.files[path].p);
delete this.files[path];
}
Module["_Zlib_Free"](pointer);
return true;
};
/**
* Close & remove all used memory in archive
* @returns {undefined}
*/
ZLib.prototype.close = function()
{
if (!this.isModuleInit || !this.engine)
return;
for (var i in this.files)
{
if (this.files[i] && this.files[i].p)
Module["_Zlib_Free"](this.files[i].p);
}
this.files = {};
if (this.engine)
Module["_Zlib_Free"](this.engine);
this.engine = 0;
};
window.nativeZlibEngine = new ZLib();
window.onEngineInit = function()
{
window.nativeZlibEngine.isModuleInit = true;
};
})(window, undefined);

View File

@ -4,7 +4,7 @@
"wasm": true,
"asm": true,
"run_before": null,
"run_after": "import sys;sys.path.append(\"../../../build_tools/scripts\");import base;base.configure_common_apps();base.replaceInFile(\"./deploy/zlib.js\", \"__ATPOSTRUN__=[];\", \"__ATPOSTRUN__=[function(){self.onEngineInit();}];\");base.replaceInFile(\"./deploy/zlib.js\", \"function getBinaryPromise(){\", \"function getBinaryPromise2(){\");base.copy_file(\"./wasm/js/index.html\", \"./deploy/index.html\");base.copy_file(\"./wasm/js/code.js\", \"./deploy/code.js\")",
"run_after": "import sys;sys.path.append(\"../../../build_tools/scripts\");import base;base.configure_common_apps();base.replaceInFile(\"./deploy/zlib.js\", \"__ATPOSTRUN__=[];\", \"__ATPOSTRUN__=[function(){self.onZlibEngineInit();}];\");base.replaceInFile(\"./deploy/zlib.js\", \"function getBinaryPromise(){\", \"function getBinaryPromise2(){\");base.copy_file(\"./wasm/js/index.html\", \"./deploy/index.html\");base.copy_file(\"./wasm/js/code.js\", \"./deploy/code.js\")",
"base_js_content": "./wasm/js/zlib.js",
"compiler_flags": [