Skip to content

Commit

Permalink
Respect configured target profile for histogram in VS Code (#1565)
Browse files Browse the repository at this point in the history
This updates the call to `run` to that is used by the histogram feature
to take the current target profile, such that histograms in the VS Code
extension correctly compile the code with the configured target.
  • Loading branch information
swernli authored May 23, 2024
1 parent ec6df4a commit cfab56c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions npm/qsharp/src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export class Compiler implements ICompiler {
eventHandler: IQscEventTarget,
): Promise<void> {
let sources;
let profile: TargetProfile = "unrestricted";
let languageFeatures: string[] = [];

if (Array.isArray(sourcesOrConfig)) {
Expand All @@ -249,6 +250,7 @@ export class Compiler implements ICompiler {
// this is the new API
sources = sourcesOrConfig.sources;
languageFeatures = sourcesOrConfig.languageFeatures || [];
profile = sourcesOrConfig.profile || "unrestricted";
}
// All results are communicated as events, but if there is a compiler error (e.g. an invalid
// entry expression or similar), it may throw on run. The caller should expect this promise
Expand All @@ -259,6 +261,7 @@ export class Compiler implements ICompiler {
(msg: string) => onCompilerEvent(msg, eventHandler!),
shots!,
languageFeatures,
profile,
);
}

Expand Down
2 changes: 2 additions & 0 deletions vscode/src/webviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { showDocumentationCommand } from "./documentation";
import { loadProject } from "./projectSystem";
import { EventType, sendTelemetryEvent } from "./telemetry";
import { getRandomGuid } from "./utils";
import { getTarget } from "./config";

const QSharpWebViewType = "qsharp-webview";
const compilerRunTimeoutMs = 1000 * 60 * 5; // 5 minutes
Expand Down Expand Up @@ -359,6 +360,7 @@ export function registerWebViewCommands(context: ExtensionContext) {
const config = {
sources,
languageFeatures,
profile: getTarget(),
};
await worker.run(config, "", parseInt(numberOfShots), evtTarget);
sendTelemetryEvent(
Expand Down
14 changes: 12 additions & 2 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn run_internal_with_features<F>(
event_cb: F,
shots: u32,
language_features: LanguageFeatures,
capabilities: TargetCapabilityFlags,
) -> Result<(), Box<interpret::Error>>
where
F: FnMut(&str),
Expand All @@ -311,7 +312,7 @@ where
true,
sources,
PackageType::Exe,
Profile::Unrestricted.into(),
capabilities,
language_features,
) {
Ok(interpreter) => interpreter,
Expand Down Expand Up @@ -353,6 +354,7 @@ pub fn run(
event_cb: &js_sys::Function,
shots: u32,
language_features: Vec<String>,
profile: &str,
) -> Result<bool, JsValue> {
if !event_cb.is_function() {
return Err(JsError::new("Events callback function must be provided").into());
Expand All @@ -365,7 +367,15 @@ pub fn run(
// See example at https://rustwasm.github.io/wasm-bindgen/reference/receiving-js-closures-in-rust.html
let _ = event_cb.call1(&JsValue::null(), &JsValue::from(msg));
};
match run_internal_with_features(sources, event_cb, shots, language_features) {
match run_internal_with_features(
sources,
event_cb,
shots,
language_features,
Profile::from_str(profile)
.map_err(|()| format!("Invalid target profile {profile}"))?
.into(),
) {
Ok(()) => Ok(true),
Err(e) => Err(JsError::from(e).into()),
}
Expand Down
8 changes: 7 additions & 1 deletion wasm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ fn run_internal<F>(sources: SourceMap, event_cb: F, shots: u32) -> Result<(), Bo
where
F: FnMut(&str),
{
run_internal_with_features(sources, event_cb, shots, LanguageFeatures::default())
run_internal_with_features(
sources,
event_cb,
shots,
LanguageFeatures::default(),
TargetCapabilityFlags::all(),
)
}

#[test]
Expand Down

0 comments on commit cfab56c

Please sign in to comment.