Skip to content

Commit f77d028

Browse files
andrewdacenkofacebook-github-bot
authored andcommitted
//xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util:utilAndroid (#43973)
Summary: Pull Request resolved: #43973 Changelog: [Internal] Reviewed By: cortinico Differential Revision: D55725614 fbshipit-source-id: da5b06758c928e8ffe935f646b0f802cf8a4fda0
1 parent 64ed820 commit f77d028

6 files changed

Lines changed: 115 additions & 123 deletions

File tree

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5737,18 +5737,19 @@ public abstract interface class com/facebook/react/uimanager/util/ReactFindViewU
57375737
public abstract fun onViewFound (Landroid/view/View;)V
57385738
}
57395739

5740-
public class com/facebook/react/util/ExceptionDataHelper {
5741-
public fun <init> ()V
5742-
public static fun getExtraDataAsJson (Lcom/facebook/react/bridge/ReadableMap;)Ljava/lang/String;
5740+
public final class com/facebook/react/util/ExceptionDataHelper {
5741+
public static final field EXTRA_DATA_FIELD Ljava/lang/String;
5742+
public static final field INSTANCE Lcom/facebook/react/util/ExceptionDataHelper;
5743+
public static final fun getExtraDataAsJson (Lcom/facebook/react/bridge/ReadableMap;)Ljava/lang/String;
57435744
}
57445745

5745-
public class com/facebook/react/util/JSStackTrace {
5746+
public final class com/facebook/react/util/JSStackTrace {
57465747
public static final field COLUMN_KEY Ljava/lang/String;
57475748
public static final field FILE_KEY Ljava/lang/String;
5749+
public static final field INSTANCE Lcom/facebook/react/util/JSStackTrace;
57485750
public static final field LINE_NUMBER_KEY Ljava/lang/String;
57495751
public static final field METHOD_NAME_KEY Ljava/lang/String;
5750-
public fun <init> ()V
5751-
public static fun format (Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)Ljava/lang/String;
5752+
public static final fun format (Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)Ljava/lang/String;
57525753
}
57535754

57545755
public abstract interface class com/facebook/react/util/RCTLog : com/facebook/react/bridge/JavaScriptModule {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util/ExceptionDataHelper.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.util
9+
10+
import android.util.JsonWriter
11+
import com.facebook.react.bridge.JsonWriterHelper
12+
import com.facebook.react.bridge.ReadableMap
13+
import com.facebook.react.bridge.ReadableType
14+
import java.io.IOException
15+
import java.io.StringWriter
16+
17+
public object ExceptionDataHelper {
18+
19+
public const val EXTRA_DATA_FIELD: String = "extraData"
20+
21+
@JvmStatic
22+
public fun getExtraDataAsJson(metadata: ReadableMap?): String? {
23+
if (metadata == null || metadata.getType(EXTRA_DATA_FIELD) == ReadableType.Null) {
24+
return null
25+
}
26+
try {
27+
val extraDataWriter = StringWriter()
28+
val json = JsonWriter(extraDataWriter)
29+
JsonWriterHelper.value(json, metadata.getDynamic(EXTRA_DATA_FIELD))
30+
json.close()
31+
extraDataWriter.close()
32+
return extraDataWriter.toString()
33+
} catch (ex: IOException) {}
34+
return null
35+
}
36+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util/JSStackTrace.java

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.util
9+
10+
import com.facebook.react.bridge.ReadableArray
11+
import com.facebook.react.bridge.ReadableMap
12+
import com.facebook.react.bridge.ReadableType
13+
import java.util.regex.Pattern
14+
15+
public object JSStackTrace {
16+
17+
public const val LINE_NUMBER_KEY: String = "lineNumber"
18+
public const val FILE_KEY: String = "file"
19+
public const val COLUMN_KEY: String = "column"
20+
public const val METHOD_NAME_KEY: String = "methodName"
21+
private val FILE_ID_PATTERN = Pattern.compile("\\b((?:seg-\\d+(?:_\\d+)?|\\d+)\\.js)")
22+
23+
@JvmStatic
24+
public fun format(message: String, stack: ReadableArray): String {
25+
val stringBuilder = StringBuilder(message).append(", stack:\n")
26+
for (i in 0 until stack.size()) {
27+
val frame = stack.getMap(i)
28+
stringBuilder.append(frame.getString(METHOD_NAME_KEY)).append("@").append(parseFileId(frame))
29+
if (frame.hasKey(LINE_NUMBER_KEY) &&
30+
!frame.isNull(LINE_NUMBER_KEY) &&
31+
frame.getType(LINE_NUMBER_KEY) == ReadableType.Number) {
32+
stringBuilder.append(frame.getInt(LINE_NUMBER_KEY))
33+
} else {
34+
stringBuilder.append(-1)
35+
}
36+
if (frame.hasKey(COLUMN_KEY) &&
37+
!frame.isNull(COLUMN_KEY) &&
38+
frame.getType(COLUMN_KEY) == ReadableType.Number) {
39+
stringBuilder.append(":").append(frame.getInt(COLUMN_KEY))
40+
}
41+
stringBuilder.append("\n")
42+
}
43+
return stringBuilder.toString()
44+
}
45+
46+
// Besides a regular bundle (e.g. "bundle.js"), a stack frame can be produced by:
47+
// 1) "random access bundle (RAM)", e.g. "1.js", where "1" is a module name
48+
// 2) "segment file", e.g. "seg-1.js", where "1" is a segment name
49+
// 3) "RAM segment file", e.g. "seg-1_2.js", where "1" is a segment name and "2" is a module name
50+
// We are using a special source map format for such cases, so that we could symbolicate
51+
// stack traces with a single source map file.
52+
// NOTE: The ".js" suffix is kept to avoid ambiguities between "module-id:line" and "line:column".
53+
private fun parseFileId(frame: ReadableMap): String {
54+
if (frame.hasKey(FILE_KEY) &&
55+
!frame.isNull(FILE_KEY) &&
56+
frame.getType(FILE_KEY) == ReadableType.String) {
57+
val file = frame.getString(FILE_KEY)
58+
if (file != null) {
59+
val matcher = FILE_ID_PATTERN.matcher(file)
60+
if (matcher.find()) {
61+
return "${matcher.group(1)}:"
62+
}
63+
}
64+
}
65+
return ""
66+
}
67+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util/RCTLog.java renamed to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/util/RCTLog.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
package com.facebook.react.util;
8+
package com.facebook.react.util
99

10-
import com.facebook.react.bridge.JavaScriptModule;
10+
import com.facebook.react.bridge.JavaScriptModule
1111

1212
/**
1313
* JS module interface for RCTLog
1414
*
15-
* <p>The RCTLog module allows for showing native logs in JavaScript.
15+
* The RCTLog module allows for showing native logs in JavaScript.
1616
*/
17-
public interface RCTLog extends JavaScriptModule {
18-
17+
public interface RCTLog : JavaScriptModule {
1918
/**
2019
* Send a log to JavaScript.
2120
*
2221
* @param level The level of the log.
2322
* @param message The message to log.
2423
*/
25-
void logIfNoNativeHook(String level, String message);
24+
public fun logIfNoNativeHook(level: String?, message: String?)
2625
}

0 commit comments

Comments
 (0)