[@types/frida-gum]: update: make Java.Wrapper more generic (#42826)

* [@types/frida-gum]: update: make `Java.Wrapper` more generic

* [@types/frida-gum]: update: bump version

* [@types/frida-gum]: update: improved generic types
1. more generics, like `Java.cast`, `Java.Field`, etc.
2. `this` of `Java.MethodImplementation` is now strictly typed so it's no longer necessary to specify the generic type of implementations explicitly
3. rename `Properties` to `Members`
4. fix JSDoc of `Java.retain` and `Java.ClassFactory.retain`
This commit is contained in:
Andrew Casey
2020-03-05 10:37:45 -08:00
committed by GitHub
parent 52658e3c69
commit 46ff792525
2 changed files with 72 additions and 28 deletions
+33 -2
View File
@@ -57,6 +57,37 @@ Java.enumerateClassLoadersSync()
.forEach(classLoader => {
// $ExpectType ClassFactory
const factory = Java.ClassFactory.get(classLoader);
// $ExpectType Wrapper
factory.use("java.lang.String");
interface Props {
myMethod: Java.MethodDispatcher;
myField: Java.Field<number>;
}
// $ExpectType Wrapper<Props>
const MyJavaClass = factory.use<Props>("my.java.class");
// $ExpectError
factory.use<{ illegal: string }>("");
// $ExpectType string
MyJavaClass.$className;
// $ExpectType MethodDispatcher<Props>
MyJavaClass.myMethod;
// $ExpectType Wrapper<Props>
MyJavaClass.myMethod.holder;
// $ExpectType Wrapper<Props>
MyJavaClass.myMethod.holder.myField.holder.myMethod.holder;
MyJavaClass.myMethod.implementation = function(...args) {
// $ExpectType MethodDispatcher<Props>
this.myMethod;
// $ExpectType Field<number, Props>
this.myField;
// $ExpectType number
this.myField.value;
};
// $ExpectType Wrapper<Props>
Java.retain(MyJavaClass);
interface AnotherProps {
anotherMethod: Java.MethodDispatcher;
anotherField: Java.Field<string>;
}
const MyAnotherJavaClass = factory.use<AnotherProps>("my.another.java.class");
// $ExpectType Wrapper<AnotherProps>
Java.cast(MyJavaClass, MyAnotherJavaClass);
});
+39 -26
View File
@@ -1,8 +1,8 @@
// Type definitions for non-npm package frida-gum 15.0
// Type definitions for non-npm package frida-gum 15.1
// Project: https://github.com/frida/frida
// Definitions by: Ole André Vadla Ravnås <https://github.com/oleavr>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
// Minimum TypeScript Version: 3.5
/**
* Returns a hexdump of the provided ArrayBuffer or NativePointerValue target.
@@ -4096,7 +4096,7 @@ declare namespace Java {
*
* @param className Canonical class name to get a wrapper for.
*/
function use(className: string): Wrapper;
function use<T extends Members<T> = {}>(className: string): Wrapper<T>;
/**
* Opens the .dex file at `filePath`.
@@ -4117,9 +4117,9 @@ declare namespace Java {
/**
* Duplicates a JavaScript wrapper for later use outside replacement method.
*
* @param handle An existing wrapper retrieved from `this` in replacement method.
* @param obj An existing wrapper retrieved from `this` in replacement method.
*/
function retain(obj: Wrapper): Wrapper;
function retain<T extends Members<T> = {}>(obj: Wrapper<T>): Wrapper<T>;
/**
* Creates a JavaScript wrapper given the existing instance at `handle` of
@@ -4128,7 +4128,10 @@ declare namespace Java {
* @param handle An existing wrapper or a JNI handle.
* @param klass Class wrapper for type to cast to.
*/
function cast(handle: Wrapper | NativePointerValue, klass: Wrapper): Wrapper;
function cast<From extends Members<From> = {}, To extends Members<To> = {}>(
handle: Wrapper<From> | NativePointerValue,
klass: Wrapper<To>
): Wrapper<To>;
/**
* Creates a Java array with elements of the specified `type`, from a
@@ -4214,23 +4217,30 @@ declare namespace Java {
onComplete: () => void;
}
type Members<T> = Record<keyof T, MethodDispatcher | Field>;
/**
* Dynamically generated wrapper for any Java class, instance, or interface.
*/
interface Wrapper {
type Wrapper<T extends Members<T> = {}> = {
/**
* Automatically inject holder's type to all fields and methods
*/
[K in keyof T]: T[K] extends Field<infer Value> ? Field<Value, T> : MethodDispatcher<T>
} & {
/**
* Allocates and initializes a new instance of the given class.
*
* Use this to create a new instance.
*/
$new: MethodDispatcher;
$new: MethodDispatcher<T>;
/**
* Allocates a new instance without initializing it.
*
* Call `$init()` to initialize it.
*/
$alloc: MethodDispatcher;
$alloc: MethodDispatcher<T>;
/**
* Initializes an instance that was allocated but not yet initialized.
@@ -4238,7 +4248,7 @@ declare namespace Java {
*
* Replace the `implementation` property to hook a given constructor.
*/
$init: MethodDispatcher;
$init: MethodDispatcher<T>;
/**
* Retrieves a `java.lang.Class` wrapper for the current class.
@@ -4259,13 +4269,13 @@ declare namespace Java {
* Methods and fields.
*/
[name: string]: any;
}
};
interface MethodDispatcher extends Method {
interface MethodDispatcher<Holder extends Members<Holder> = {}> extends Method<Holder> {
/**
* Available overloads.
*/
overloads: Method[];
overloads: Array<Method<Holder>>;
/**
* Obtains a specific overload.
@@ -4273,10 +4283,10 @@ declare namespace Java {
* @param args Signature of the overload to obtain.
* For example: `"java.lang.String", "int"`.
*/
overload(...args: string[]): Method;
overload(...args: string[]): Method<Holder>;
}
interface Method {
interface Method<Holder extends Members<Holder> = {}> {
(...params: any[]): any;
/**
@@ -4287,7 +4297,7 @@ declare namespace Java {
/**
* Class that this method belongs to.
*/
holder: Wrapper;
holder: Wrapper<Holder>;
/**
* What kind of method this is, i.e. constructor vs static vs instance.
@@ -4304,7 +4314,7 @@ declare namespace Java {
* replace the original implementation. Assign `null` at a future point
* to revert back to the original implementation.
*/
implementation: MethodImplementation | null;
implementation: MethodImplementation<Holder> | null;
/**
* Method return type.
@@ -4327,21 +4337,21 @@ declare namespace Java {
* Useful for e.g. setting `traps: "all"` to perform execution tracing
* in conjunction with Stalker.
*/
clone: (options: NativeFunctionOptions) => Method;
clone: (options: NativeFunctionOptions) => Method<Holder>;
}
type MethodImplementation = (this: Wrapper, ...params: any[]) => any;
type MethodImplementation<This extends Members<This> = {}> = (this: Wrapper<This>, ...params: any[]) => any;
interface Field {
interface Field<Value = any, Holder extends Members<Holder> = {}> {
/**
* Current value of this field. Assign to update the field's value.
*/
value: any;
value: Value;
/**
* Class that this field belongs to.
*/
holder: Wrapper;
holder: Wrapper<Holder>;
/**
* What kind of field this is, i.e. static vs instance.
@@ -4545,7 +4555,7 @@ declare namespace Java {
*
* @param className Canonical class name to get a wrapper for.
*/
use(className: string): Wrapper;
use<T extends Members<T> = {}>(className: string): Wrapper<T>;
/**
* Opens the .dex file at `filePath`.
@@ -4566,9 +4576,9 @@ declare namespace Java {
/**
* Duplicates a JavaScript wrapper for later use outside replacement method.
*
* @param handle An existing wrapper retrieved from `this` in replacement method.
* @param obj An existing wrapper retrieved from `this` in replacement method.
*/
retain(obj: Wrapper): Wrapper;
retain<T extends Members<T> = {}>(obj: Wrapper<T>): Wrapper<T>;
/**
* Creates a JavaScript wrapper given the existing instance at `handle` of
@@ -4577,7 +4587,10 @@ declare namespace Java {
* @param handle An existing wrapper or a JNI handle.
* @param klass Class wrapper for type to cast to.
*/
cast(handle: Wrapper | NativePointerValue, klass: Wrapper): Wrapper;
cast<From extends Members<From> = {}, To extends Members<To> = {}>(
handle: Wrapper<From> | NativePointerValue,
klass: Wrapper<To>
): Wrapper<To>;
/**
* Creates a Java array with elements of the specified `type`, from a