From 7501ee3c9e67f0921201e7c88e98e89c2df979a2 Mon Sep 17 00:00:00 2001 From: Yuxiao Mao Date: Thu, 30 May 2024 09:37:27 +0200 Subject: [PATCH] Back to standalone adapter.js for compound launch Support multiple HLAdapter+API instances in the same vscode window (same Extension). --- .vscodeignore | 1 + Makefile | 3 ++- package.json | 1 + src/Extension.hx | 32 ++++++++++++++++++++++++++++---- src/HLAdapter.hx | 39 +++++++++++++++++++++++++-------------- 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/.vscodeignore b/.vscodeignore index d88f487..3d6b0c0 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -1,6 +1,7 @@ * */** !node_modules/**/* +!adapter.js !bindings.js !CHANGELOG.md !extension.js diff --git a/Makefile b/Makefile index 6b66f01..b62d768 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,8 @@ cleanup: find . -name *.tlog | xargs rm -rf find . -name *.map | xargs rm -rf build: - haxe -cp src -lib vscode -lib vshaxe -lib vscode-debugadapter -lib format -lib hscript -D js-es=6 -js extension.js Extension + haxe -cp src -lib vscode -lib vshaxe -lib vscode-debugadapter -D js-es=6 -js extension.js Extension + haxe build.hxml package: cleanup build #npm install vsce -g vsce package diff --git a/package.json b/package.json index 797d9ea..b603e50 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ { "type": "hl", "label": "HashLink", + "program": "./adapter.js", "runtime": "node", "configurationAttributes": { "launch": { diff --git a/src/Extension.hx b/src/Extension.hx index b3c1624..2a9f8a8 100644 --- a/src/Extension.hx +++ b/src/Extension.hx @@ -7,7 +7,7 @@ class Extension { static function main(context:ExtensionContext) { Vscode.debug.registerDebugConfigurationProvider("hl", {resolveDebugConfiguration: resolveDebugConfiguration}); Vscode.debug.registerDebugAdapterDescriptorFactory("hl", {createDebugAdapterDescriptor: createDebugAdapterDescriptor}); - context.subscriptions.push(Vscode.commands.registerCommand("hldebug.var.formatInt", args -> HLAdapter.inst?.formatInt(args))); + context.subscriptions.push(Vscode.commands.registerCommand("hldebug.var.formatInt", args -> formatInt(args))); } static function resolveDebugConfiguration(folder:Null, config:DebugConfiguration, @@ -63,8 +63,32 @@ class Extension { static function createDebugAdapterDescriptor(session: DebugSession, ?executable:DebugAdapterExecutable): ProviderResult { var config = Vscode.workspace.getConfiguration("hldebug"); - HLAdapter.DEBUG = config.get("verbose", false); - var adapter = new HLAdapter(config.get("defaultPort", 6112)); - return new vscode.DebugAdapterInlineImplementation(cast adapter); + var isVerbose = config.get("verbose", false); + var defaultPort = config.get("defaultPort", 6112); + + /* + // Can be used to communicate with one built-in adapter during execution. Build with -lib format -lib hscript. + if( HLAdapter.inst == null ) { + HLAdapter.DEBUG = isVerbose; + HLAdapter.DEFAULT_PORT = defaultPort; + var adapter = new HLAdapter(); + return new vscode.DebugAdapterInlineImplementation(cast adapter); + } + */ + + if( executable == null ) + Vscode.window.showErrorMessage("No executable specified. Please check your configuration."); + if( isVerbose ) + executable.args.push("--verbose"); + executable.args.push("--defaultPort"); + executable.args.push("" + defaultPort); + return executable; + } + + static function formatInt( args:VariableContextCommandArg ) { + var i = Std.parseInt(args.variable.value); + if (i == null) + return; + Vscode.window.showInformationMessage(args.variable.name + "(" + i + ") = 0x" + Utils.toString(i,16) + " = 0b" + Utils.toString(i,2)); } } diff --git a/src/HLAdapter.hx b/src/HLAdapter.hx index 15edffa..c8c6742 100644 --- a/src/HLAdapter.hx +++ b/src/HLAdapter.hx @@ -21,6 +21,7 @@ class HLAdapter extends DebugSession { static var UID = 0; public static var inst : HLAdapter; public static var DEBUG = false; + public static var DEFAULT_PORT : Int = 6112; var proc : ChildProcessObject; var workspaceDirectory : String; @@ -43,10 +44,10 @@ class HLAdapter extends DebugSession { static var isWindows = Sys.systemName() == "Windows"; static var isMac = Sys.systemName() == "Mac"; - public function new( defaultPort : Int = 6112 ) { + public function new() { super(); allowEvalGetters = false; - debugPort = defaultPort; + debugPort = DEFAULT_PORT; doDebug = true; threads = new Map(); startTime = haxe.Timer.stamp(); @@ -1087,7 +1088,28 @@ class HLAdapter extends DebugSession { // Standalone adapter.js static function main() { - if( DEBUG ) { + function paramError( msg ) { + Sys.stderr().writeString(msg + "\n"); + // Sys.exit(1); // Ignore error + } + + var args = Sys.args(); + while( args.length > 0 && args[0].charCodeAt(0) == '-'.code ) { + var param = args.shift(); + switch( param ) { + case "--verbose": + HLAdapter.DEBUG = true; + case "--defaultPort": + param = args.shift(); + var port : Int; + if( param != null && (port = Std.parseInt(param)) != 0 ) + HLAdapter.DEFAULT_PORT = port; + paramError("Require defaultPort int value"); + default: + paramError("Unsupported parameter " + param); + } + } + if( HLAdapter.DEBUG ) { js.Node.process.on("uncaughtException", function(e:js.lib.Error) { if( inst != null ) inst.sendEvent(new OutputEvent("*** ERROR *** " +e.message+"\n"+e.stack, Stderr)); Sys.exit(1); @@ -1096,15 +1118,4 @@ class HLAdapter extends DebugSession { DebugSession.run( HLAdapter ); } - // Communicate with Extension - - #if vscode - public function formatInt( args:VariableContextCommandArg ) { - var i = Std.parseInt(args.variable.value); - if (i == null) - return; - Vscode.window.showInformationMessage(args.variable.name + "(" + i + ") = 0x" + Utils.toString(i,16) + " = 0b" + Utils.toString(i,2)); - } - #end - }