Skip to content

Commit

Permalink
upload nativecode
Browse files Browse the repository at this point in the history
  • Loading branch information
thqby committed Feb 12, 2022
1 parent cd80abe commit 6dbbb42
Show file tree
Hide file tree
Showing 6 changed files with 719 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Audio/Audio.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @file: Audio.ahk
* @description: Core Audio APIs, Windows 多媒体设备API
* @author thqby
* @date 2021/10/11
* @version 1.0.11
* @date 2022/02/09
* @version 1.0.12
***********************************************************************/
; https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown
class IAudioBase {
Expand Down Expand Up @@ -91,7 +91,7 @@ class IMMDeviceEnumerator extends IAudioBase {
*/
EnumAudioEndpoints(dataFlow := 0, dwStateMask := 1) => (ComCall(3, this, "Int", dataFlow, "UInt", dwStateMask, "Ptr*", &pDevices := 0), IMMDeviceCollection(pDevices))
GetDefaultAudioEndpoint(dataFlow := 0, role := 0) => (ComCall(4, this, "Int", dataFlow, "UInt", role, "Ptr*", &pEndpoint := 0), IMMDevice(pEndpoint))
GetDevice(pwstrId) => (ComCall(5, this, "Str", pwstrId, "Ptr*", &pEndpoint := 0), IMMDevice(pEndpoint, this))
GetDevice(pwstrId) => (ComCall(5, this, "Str", pwstrId, "Ptr*", &pEndpoint := 0), IMMDevice(pEndpoint))
RegisterEndpointNotificationCallback(pClient) => ComCall(6, this, "Ptr", pClient)
UnregisterEndpointNotificationCallback(pClient) => ComCall(7, this, "Ptr", pClient)
}
Expand Down
80 changes: 80 additions & 0 deletions NativeCode.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Generate a function with native code
* @param BIF Function addresses, `void funcname(ResultToken &aResultToken, ExprTokenType *aParam[], int aParamCount)`
* @param MinParams The number of required parameters
* @param ParamCount The number of maximum parameters, ParamCount = 255 if the function is variadic
* @param OutputVars The array that contains one-based indexs of outputvars, up to seven
* @param FID Function ID, `aResultToken.func->mFID`, for code sharing: this function's ID in the group of functions which share the same C++ function
*/
BuiltInFunc(BIF, MinParams := 0, ParamCount := 0, OutputVars := 0, FID := 0) {
static p__init := ObjPtr(Any.__Init), size := 8 * A_PtrSize + 16
; if a func obj has not own propertys, can improve `Call` performance, so use caching to store the BIF structure
static bifcache := Map(), _ := %A_ThisFunc%.DefineProp('free', { call: (s, obj) => bifcache.Delete(IsObject(obj) ? ObjPtr(obj) : obj) })
; copy a func obj struct
sbif := Buffer(OutputVars ? size + 7 : size, 0), DllCall('RtlMoveMemory', 'ptr', sbif, 'ptr', p__init, 'uint', size)
obif := ObjFromPtr(sbif.Ptr), IsVariadic := ParamCount == 255 ; MAXP_VARIADIC
bifcache[sbif.Ptr] := sbif
NumPut('uint', 1, sbif, A_PtrSize), ObjPtrAddRef(Func.Prototype) ; init func refcount and addref base obj
; init func infos
NumPut('ptr', StrPtr('User-BIF'), 'int', Max(MinParams, ParamCount), 'int', MinParams, 'int', IsVariadic, sbif, 3 * A_PtrSize + 8)
NumPut('ptr', BIF, 'ptr', FID, sbif, 6 * A_PtrSize + 16)
if OutputVars {
NumPut('ptr', s := sbif.Ptr + size, sbif, 5 * A_PtrSize + 16) ; mOutputVars
loop Min(OutputVars.Length, 7) ; MAX_FUNC_OUTPUT_VAR = 7
s := NumPut('uchar', OutputVars[A_Index], s)
}
return obif
}

/**
* Defines a new own property with native code, is similar with `obj.DefineProp`
* @param obj Any object
* @param name The name of the property
* @param desc An object with one of following own properties, or both `Get` and `Set`
*
* `Call, Get, Set`: an object with `BIM` property and optional properties `MinParams`, `ParamCount`, `OutputVars`, `MID`, is same with the parameters of `BuiltInFunc`
*
* `BIM`: `void (IObject::* ObjectMethod)(ResultToken& aResultToken, int aID, int aFlags, ExprTokenType* aParam[], int aParamCount)`
*/
ObjDefineBuiltInProp(obj, name, desc) {
static bimcache := Map(), _ := %A_ThisFunc%.DefineProp('free', { call: (s, obj, name := unset) => (obj := IsObject(obj) ? ObjPtr(obj) : obj, IsSet(name) ? bimcache[obj].DeleteProp(name) : bimcache.Delete(obj)) })
descobj := {}, baseobj := ObjPtr(obj.Base)
for MIT in ['call', 'set', 'get']
if desc.HasOwnProp(MIT) {
t := desc.%MIT%
MinParams := ParamCount := OutputVars := MID := 0, BIM := t.BIM
for k in ['MinParams', 'ParamCount', 'OutputVars', 'MID']
if t.HasOwnProp(k)
%k% := t.%k%
descobj.%MIT% := BuiltInMethod(baseobj, BIM, MIT, MinParams, ParamCount, OutputVars, MID)
}
obj.DefineProp(name, descobj), pobj := ObjPtr(obj)
cache := bimcache.Has(pobj) ? bimcache[pobj] : (bimcache[pobj] := {})
cache.%name% := descobj
BuiltInMethod(pobj, BIM, MIT, MinParams, ParamCount, OutputVars, MID) {
static pCall := ObjPtr({}.OwnProps), size := 9 * A_PtrSize + 16
nameoffset := 3 * A_PtrSize + 8, IsVariadic := ParamCount == 255
sbim := Buffer(OutputVars ? size + 7 : size, 0), DllCall('RtlMoveMemory', 'ptr', sbim, 'ptr', pCall, 'uint', size)
obim := ObjFromPtr(sbim.Ptr), obim.bim := sbim
switch MIT, false {
case 'call':
++MinParams, ParamCount := IsVariadic ? MinParams : Max(MinParams, ParamCount + 1)
NumPut('ptr', StrPtr('User-BIM.Call'), sbim, nameoffset), MIT := 2
case 'set':
MinParams += 2, ParamCount += 2, MIT := 1
NumPut('ptr', StrPtr('User-BIM.Set'), sbim, nameoffset)
case 'get':
++MinParams, ParamCount := Max(MinParams, ParamCount + 1)
NumPut('ptr', StrPtr('User-BIM.Get'), sbim, nameoffset), MIT := 0
}
NumPut('uint', 1, sbim, A_PtrSize), ObjPtrAddRef(Func.Prototype)
NumPut('int', Max(MinParams, ParamCount), 'int', MinParams, 'int', IsVariadic, sbim, 4 * A_PtrSize + 8)
NumPut('ptr', BIM, 'ptr', pobj, 'uchar', MID, 'uchar', MIT, sbim, 6 * A_PtrSize + 16)
if OutputVars {
NumPut('ptr', s := sbim.Ptr + size, sbim, 5 * A_PtrSize + 16)
loop Min(OutputVars.Length, 7)
s := NumPut('uchar', OutputVars[A_Index], s)
}
return obim
}
}
4 changes: 4 additions & 0 deletions QPC.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
QPC() {
static c := 0, f := (DllCall("QueryPerformanceFrequency", "int64*", &c), c /= 1000)
return (DllCall("QueryPerformanceCounter", "int64*", &c), c / f)
}
10 changes: 5 additions & 5 deletions SQLite/CSQLite.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* @description SQLite class
* @file CSQLite.ahk
* @author thqby
* @date 2021/10/12
* @version 0.0.2
* @date 2022/02/09
* @version 0.0.3
***********************************************************************/
class CSQLite
Expand Down Expand Up @@ -144,8 +144,8 @@ class CSQLite
if (RC := DllCall("SQLite3.dll\sqlite3_open_v2", "Ptr", StrPtr(UTF8), "Ptr*", &HDB:=0, "Int", Flags, "Ptr", 0, "Cdecl Int"))
return (this._Path := "", this.ErrorMsg := this._ErrMsg(), this.ErrorCode := RC, false)
this._Handle := HDB
this.createScalarFunction(Func("regexp"), 2)
this.createScalarFunction(Func("regex_replace"), 3)
this.createScalarFunction(regexp, 2)
this.createScalarFunction(regex_replace, 3)
return true
}
; ===================================================================================================================
Expand Down Expand Up @@ -239,7 +239,7 @@ class CSQLite
if (this._Thread.pexec)
return
this._Thread.DB:=this, this._Thread.pexec:=CSQLite.execfunc_ptr
this._Thread._Threadobj:=CSQLite.Thread.Call(Func("_Exec"), this._Thread)
this._Thread._Threadobj:=CSQLite.Thread(_Exec, this._Thread)
_Exec(pinfo){
_Thread:=ObjFromPtr(pinfo), hdb:=_Thread.DB._Handle, pexec:=_Thread.pexec
Expand Down
Loading

0 comments on commit 6dbbb42

Please sign in to comment.