diff --git a/types/frida-gum/frida-gum-tests.ts b/types/frida-gum/frida-gum-tests.ts index 87dedce183..51bf3450fc 100644 --- a/types/frida-gum/frida-gum-tests.ts +++ b/types/frida-gum/frida-gum-tests.ts @@ -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; + } + // $ExpectType Wrapper + const MyJavaClass = factory.use("my.java.class"); + // $ExpectError + factory.use<{ illegal: string }>(""); + // $ExpectType string + MyJavaClass.$className; + // $ExpectType MethodDispatcher + MyJavaClass.myMethod; + // $ExpectType Wrapper + MyJavaClass.myMethod.holder; + // $ExpectType Wrapper + MyJavaClass.myMethod.holder.myField.holder.myMethod.holder; + MyJavaClass.myMethod.implementation = function(...args) { + // $ExpectType MethodDispatcher + this.myMethod; + // $ExpectType Field + this.myField; + // $ExpectType number + this.myField.value; + }; + // $ExpectType Wrapper + Java.retain(MyJavaClass); + interface AnotherProps { + anotherMethod: Java.MethodDispatcher; + anotherField: Java.Field; + } + const MyAnotherJavaClass = factory.use("my.another.java.class"); + // $ExpectType Wrapper + Java.cast(MyJavaClass, MyAnotherJavaClass); }); diff --git a/types/frida-gum/index.d.ts b/types/frida-gum/index.d.ts index 3275c16082..f628f4829f 100644 --- a/types/frida-gum/index.d.ts +++ b/types/frida-gum/index.d.ts @@ -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 // 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 = {}>(className: string): Wrapper; /** * 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 = {}>(obj: Wrapper): Wrapper; /** * 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 = {}, To extends Members = {}>( + handle: Wrapper | NativePointerValue, + klass: Wrapper + ): Wrapper; /** * Creates a Java array with elements of the specified `type`, from a @@ -4214,23 +4217,30 @@ declare namespace Java { onComplete: () => void; } + type Members = Record; + /** * Dynamically generated wrapper for any Java class, instance, or interface. */ - interface Wrapper { + type Wrapper = {}> = { + /** + * Automatically inject holder's type to all fields and methods + */ + [K in keyof T]: T[K] extends Field ? Field : MethodDispatcher + } & { /** * Allocates and initializes a new instance of the given class. * * Use this to create a new instance. */ - $new: MethodDispatcher; + $new: MethodDispatcher; /** * Allocates a new instance without initializing it. * * Call `$init()` to initialize it. */ - $alloc: MethodDispatcher; + $alloc: MethodDispatcher; /** * 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; /** * 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 = {}> extends Method { /** * Available overloads. */ - overloads: Method[]; + overloads: Array>; /** * 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; } - interface Method { + interface Method = {}> { (...params: any[]): any; /** @@ -4287,7 +4297,7 @@ declare namespace Java { /** * Class that this method belongs to. */ - holder: Wrapper; + holder: Wrapper; /** * 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 | 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; } - type MethodImplementation = (this: Wrapper, ...params: any[]) => any; + type MethodImplementation = {}> = (this: Wrapper, ...params: any[]) => any; - interface Field { + interface Field = {}> { /** * 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; /** * 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 = {}>(className: string): Wrapper; /** * 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 = {}>(obj: Wrapper): Wrapper; /** * 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 = {}, To extends Members = {}>( + handle: Wrapper | NativePointerValue, + klass: Wrapper + ): Wrapper; /** * Creates a Java array with elements of the specified `type`, from a